Skip to content

Commit

Permalink
Merge pull request #3 from mepis/main
Browse files Browse the repository at this point in the history
  • Loading branch information
IPQualityScore committed Oct 17, 2023
2 parents 36a2b5d + e4ddc7a commit 5c00a3f
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 55 deletions.
Binary file added .DS_Store
Binary file not shown.
27 changes: 27 additions & 0 deletions README.md
Expand Up @@ -279,6 +279,33 @@ DeviceFingerprint.initializeScriptAsync(secretKey).then(() => {
});
```

# Define the Tracker Domain

IPQS device trackers can be tied to specific domains. This option is selected while creating the device tracker. Adding the defined domain to the device tracker only requires one additional step - pass the domain name as a second variable with the secret key.

```javascript
const secretKey = process.env.VUE_APP_IPQS_DT_KEY;
const domain = "example.com";
DeviceFingerprint.initializeScriptAsync(secretKey, domain)
.then(async () => {
DeviceFingerprint.AfterResult((result) => {
console.log("IPQS Fingerprint: ", result);
// ########################################
// Handle The Fingerprint Record
// ########################################
// Best practice is to save the fingerprint results to the Store. The
// example below saves the fingerprint results to a Vuex initialized store.

// this.$store.commit.save_fingerprint(result);
// return result;
});
DeviceFingerprint.Init();
});

</script>
```


# Need Help?

If you need additional help or would like to schedule a meeting for assistance, open a new help ticket in your [IPQS account](https://www.ipqualityscore.com/user/support/new).
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "node_js_ipqs_device_tracker",
"version": "1.0.4",
"version": "1.0.5",
"description": "NodeJS/React package to interface with the IPQS Device Fingerprint API.",
"types": "./lib/cjs/types/index.d.ts",
"main": "./lib/cjs/index.js",
Expand Down
8 changes: 4 additions & 4 deletions src/index.ts
Expand Up @@ -11,12 +11,12 @@ import {
} from "./util/deviceFingerprintFunctions";
import {addDeviceTrackingTags, addDeviceTrackingTagsAsync} from "./util/domManipulation";

const initializeScript = (secretKey: string) => {
addDeviceTrackingTags(secretKey);
const initializeScript = (secretKey: string, domain: string) => {
addDeviceTrackingTags(secretKey, domain);
}

const initializeScriptAsync = (secretKey: string) => {
return addDeviceTrackingTagsAsync(secretKey);
const initializeScriptAsync = (secretKey: string, domain: string) => {
return addDeviceTrackingTagsAsync(secretKey, domain);
}
export default {
initializeScript,
Expand Down
115 changes: 65 additions & 50 deletions src/util/domManipulation.ts
@@ -1,67 +1,82 @@
import {DEVICE_FINGERPRINT_SCRIPT_ID, DEVICE_FINGERPRINT_SCRIPT_ID_ASYNC} from "./const";
import {
DEVICE_FINGERPRINT_SCRIPT_ID,
DEVICE_FINGERPRINT_SCRIPT_ID_ASYNC,
} from "./const";

export const addDeviceTrackingTags = (secretKey: string) => {
document.head.appendChild(getScriptTagWindowIPQInit());
document.head.appendChild(getScriptTagLoadSrc(secretKey));
document.head.appendChild(getNoscriptTag(secretKey));
}
export const addDeviceTrackingTags = (secretKey: string, domain: string) => {
if (domain === undefined) domain = "*";
document.head.appendChild(getScriptTagWindowIPQInit());
document.head.appendChild(getScriptTagLoadSrc(secretKey, domain));
document.head.appendChild(getNoscriptTag(secretKey, domain));
};

export const addDeviceTrackingTagsAsync = (secretKey: string) => {
return new Promise<void>((resolve, reject) => {
document.head.appendChild(getScriptTagWindowIPQInit());
document.head.appendChild(getScriptTagLoadSrcAsync(secretKey, resolve, reject));
document.head.appendChild(getNoscriptTag(secretKey));
});
}
export const addDeviceTrackingTagsAsync = (
secretKey: string,
domain: string
) => {
return new Promise<void>((resolve, reject) => {
if (domain === undefined) domain = "*";
document.head.appendChild(getScriptTagWindowIPQInit());
document.head.appendChild(
getScriptTagLoadSrcAsync(secretKey, domain, resolve, reject)
);
document.head.appendChild(getNoscriptTag(secretKey, domain));
});
};

const getScriptTagWindowIPQInit = () => {
const scriptTag: HTMLScriptElement = document.createElement("script");
scriptTag.setAttribute("type", "text/javascript");
const inlineScript = document.createTextNode(`
const scriptTag: HTMLScriptElement = document.createElement("script");
scriptTag.setAttribute("type", "text/javascript");
const inlineScript = document.createTextNode(`
window.IPQ = {
Callback: function(){}
};
`);
scriptTag.appendChild(inlineScript);
return scriptTag;
scriptTag.appendChild(inlineScript);
return scriptTag;
};

const getScriptTagLoadSrc = (secretKey: string) => {
const srcFile = `https://www.ipqualityscore.com/api/*/${secretKey}/learn.js`;
const scriptTag: Element = document.createElement("script");
scriptTag.setAttribute('src', srcFile);
scriptTag.setAttribute('id', DEVICE_FINGERPRINT_SCRIPT_ID);
scriptTag.setAttribute('crossorigin', 'anonymous');
return scriptTag;
}
const getScriptTagLoadSrc = (secretKey: string, domain: string) => {
const srcFile = `https://www.ipqualityscore.com/api/${domain}/${secretKey}/learn.js`;
const scriptTag: Element = document.createElement("script");
scriptTag.setAttribute("src", srcFile);
scriptTag.setAttribute("id", DEVICE_FINGERPRINT_SCRIPT_ID);
scriptTag.setAttribute("crossorigin", "anonymous");
return scriptTag;
};

const getScriptTagLoadSrcAsync = (secretKey: string, resolveCb: () => void, rejectCb: () => void) => {
const srcFile = `https://www.ipqualityscore.com/api/*/${secretKey}/learn.js`;
const scriptTag: HTMLScriptElement = document.createElement("script");
scriptTag.setAttribute('src', srcFile);
scriptTag.setAttribute('id', DEVICE_FINGERPRINT_SCRIPT_ID_ASYNC);
scriptTag.setAttribute('crossorigin', 'anonymous');
// For async
scriptTag.onload = function() {
resolveCb();
};
scriptTag.onerror = function() {
rejectCb();
};
return scriptTag;
const getScriptTagLoadSrcAsync = (
secretKey: string,
domain: string,
resolveCb: () => void,
rejectCb: () => void
) => {
const srcFile = `https://www.ipqualityscore.com/api/${domain}/${secretKey}/learn.js`;
const scriptTag: HTMLScriptElement = document.createElement("script");
scriptTag.setAttribute("src", srcFile);
scriptTag.setAttribute("id", DEVICE_FINGERPRINT_SCRIPT_ID_ASYNC);
scriptTag.setAttribute("crossorigin", "anonymous");
// For async
scriptTag.onload = function () {
resolveCb();
};
scriptTag.onerror = function () {
rejectCb();
};
return scriptTag;
};

const getNoscriptTag = (secretKey: string) => {
const noscriptTag: Element = document.createElement("noscript");
noscriptTag.appendChild(generateImageTag(secretKey));
return noscriptTag;
const getNoscriptTag = (secretKey: string, domain: string) => {
const noscriptTag: Element = document.createElement("noscript");
noscriptTag.appendChild(generateImageTag(secretKey, domain));
return noscriptTag;
};

const generateImageTag = (secretKey: string) => {
const imageTag: Element = document.createElement("img");
const srcImage = `https://www.ipqscdn.com/api/*/${secretKey}/pixel.png`;
imageTag.setAttribute('src', srcImage);
return imageTag;
const generateImageTag = (secretKey: string, domain: string) => {
const imageTag: Element = document.createElement("img");
const srcImage = `https://www.ipqscdn.com/api/${domain}/${secretKey}/pixel.png`;
imageTag.setAttribute("src", srcImage);
return imageTag;
};

export default addDeviceTrackingTags;
export default addDeviceTrackingTags;

0 comments on commit 5c00a3f

Please sign in to comment.