Skip to content

Commit c70f54d

Browse files
authored
Merge pull request #20 from ingestly/add-custom-tracking
Auto-track methods now support custom context. Thanks @twofacauth for your suggestion.
2 parents 40b984a + ffbc6c8 commit c70f54d

File tree

8 files changed

+2234
-719
lines changed

8 files changed

+2234
-719
lines changed

package-lock.json

Lines changed: 1923 additions & 521 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@
2323
"homepage": "https://github.com/ingestly/ingestly-client-javascript#readme",
2424
"dependencies": {},
2525
"devDependencies": {
26-
"@babel/core": "^7.6.2",
27-
"@babel/plugin-proposal-decorators": "^7.6.0",
28-
"@babel/preset-env": "^7.6.2",
26+
"@babel/core": "^7.8.3",
27+
"@babel/plugin-proposal-decorators": "^7.8.3",
28+
"@babel/preset-env": "^7.8.3",
2929
"babel-eslint": "^10.0.3",
3030
"babel-loader": "^8.0.6",
3131
"eslint": "^5.16.0",
3232
"jsdoc": "^3.6.3",
33-
"prettier": "^1.18.2",
34-
"webpack": "^4.41.0",
35-
"webpack-cli": "^3.3.9"
33+
"prettier": "^1.19.1",
34+
"webpack": "^4.41.5",
35+
"webpack-cli": "^3.3.10"
3636
}
3737
}

src/emitter.js

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const xhr = (url, callback) => {
2121
};
2222

2323
const generateDestination = (feature, payload, deviceId, rootId) => {
24-
const timestamp = (+new Date).toString(36);
24+
const timestamp = (+new Date()).toString(36);
2525
let url = `https://${config.endpoint}/${feature}/${timestamp}/`;
2626
url += `?key=${config.apiKey}`;
2727
url += `&sdk=JS-${config.sdkVersion}`;
@@ -36,17 +36,18 @@ const generateDestination = (feature, payload, deviceId, rootId) => {
3636
};
3737

3838
const generateParamPair = (key, val) => {
39-
if(typeof val === 'object'){
39+
if (typeof val === 'object') {
4040
val = JSON.stringify(val);
4141
}
4242
if (typeof val !== 'undefined' && val !== '' && val !== '{}') {
43-
return (`&${key}=${encodeURIComponent(val)}`);
43+
return `&${key}=${encodeURIComponent(val)}`;
4444
} else {
4545
return '';
4646
}
4747
};
4848

49-
let config, status = true;
49+
let config,
50+
status = true;
5051

5152
/**
5253
* @ignore
@@ -65,10 +66,13 @@ export default class {
6566
status = false;
6667
}
6768
if (!status) {
68-
if (typeof window[config.target].fetch === 'function' && typeof window[config.target].AbortController === 'function') {
69+
if (
70+
typeof window[config.target].fetch === 'function' &&
71+
typeof window[config.target].AbortController === 'function'
72+
) {
6973
const controller = new AbortController();
7074
const signal = controller.signal;
71-
const option = {signal, method: 'POST', cache: 'no-store', keepalive: true};
75+
const option = { signal, method: 'POST', cache: 'no-store', keepalive: true };
7276
setTimeout(() => controller.abort(), 4000);
7377
window[config.target].fetch(url, option);
7478
} else {
@@ -82,22 +86,26 @@ export default class {
8286

8387
sync(payload, callback) {
8488
let url = generateDestination('ingestly-sync', payload, config.deviceId, config.rootId);
85-
if (typeof window[config.target].fetch === 'function' && typeof window[config.target].AbortController === 'function') {
89+
if (
90+
typeof window[config.target].fetch === 'function' &&
91+
typeof window[config.target].AbortController === 'function'
92+
) {
8693
const controller = new AbortController();
8794
const signal = controller.signal;
88-
const option = {signal, method: 'GET', cache: 'no-store', keepalive: true};
95+
const option = { signal, method: 'GET', cache: 'no-store', keepalive: true };
8996
setTimeout(() => controller.abort(), 4000);
90-
window[config.target].fetch(url, option).then((response) => {
97+
window[config.target]
98+
.fetch(url, option)
99+
.then(response => {
91100
return response.json();
92-
}
93-
).then((result) => {
94-
callback.call(null, result.id);
95-
});
101+
})
102+
.then(result => {
103+
callback.call(null, result.id);
104+
});
96105
} else {
97-
xhr(url, (result) => {
106+
xhr(url, result => {
98107
callback.call(null, result.id);
99108
});
100109
}
101110
}
102111
}
103-

src/events.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
let managedEvents = {}, handlerKey = 0;
1+
let managedEvents = {},
2+
handlerKey = 0;
23

34
/**
45
* @ignore
@@ -14,38 +15,38 @@ export default class {
1415
event.initCustomEvent(config.eventName, false, false, {});
1516
}
1617

17-
window.parent.requestAnimationFrame = window.parent.requestAnimationFrame
18-
|| window.parent.mozRequestAnimationFrame
19-
|| window.parent.webkitRequestAnimationFrame;
18+
window.parent.requestAnimationFrame =
19+
window.parent.requestAnimationFrame ||
20+
window.parent.mozRequestAnimationFrame ||
21+
window.parent.webkitRequestAnimationFrame;
2022

2123
(function recurringEvent() {
2224
window.parent.requestAnimationFrame(recurringEvent);
2325
if (timer) {
2426
return false;
2527
}
26-
timer = setTimeout( () => {
28+
timer = setTimeout(() => {
2729
window.parent.dispatchEvent(event);
2830
timer = null;
2931
}, config.eventFrequency);
3032
})();
3133
}
3234

33-
addListener(element, type, listener, capture){
35+
addListener(element, type, listener, capture) {
3436
element.addEventListener(type, listener, capture);
3537
managedEvents[handlerKey] = {
3638
element: element,
3739
type: type,
3840
listener: listener,
39-
capture: capture
41+
capture: capture,
4042
};
4143
return handlerKey++;
4244
}
4345

44-
removeListener(handlerKey){
46+
removeListener(handlerKey) {
4547
if (handlerKey in managedEvents) {
4648
let event = managedEvents[handlerKey];
4749
event.element.removeEventListener(event.type, event.listener, event.capture);
4850
}
4951
}
50-
51-
}
52+
}

src/idm.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
const generateId = () => {
2-
const timestamp = (+new Date).toString(36);
3-
const chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
2+
const timestamp = (+new Date()).toString(36);
3+
const chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
44
let result = '';
55
for (let i = 0; i < 32; i++) {
6-
result += chars[Math.floor(Math.random() * (chars.length))];
6+
result += chars[Math.floor(Math.random() * chars.length)];
77
}
88
return `${timestamp}-${result}`;
99
};
1010

11-
const readCookie = (key) => {
11+
const readCookie = key => {
1212
const cookies = window.parent.document.cookie || '';
13-
return ((`; ${cookies};`).match(`; ${key}=([^¥S;]*)`) || [])[1];
13+
return (`; ${cookies};`.match(`; ${key}=([^¥S;]*)`) || [])[1];
1414
};
1515

1616
const initDeviceId = () => {
1717
const idCookie = readCookie(storageKey) || '',
18-
idStorage = localStorage.getItem(storageKey) || '';
18+
idStorage = localStorage.getItem(storageKey) || '';
1919
let deviceId;
2020

2121
if (idCookie.length > 8) {
@@ -29,7 +29,9 @@ const initDeviceId = () => {
2929
return deviceId;
3030
};
3131

32-
let storageKey, initialId, isNewId = false;
32+
let storageKey,
33+
initialId,
34+
isNewId = false;
3335

3436
/**
3537
* @ignore
@@ -48,8 +50,7 @@ export default class {
4850
try {
4951
localStorage.setItem(storageKey, deviceId);
5052
} catch (e) {
51-
window.parent.document.cookie
52-
= `${storageKey}=${deviceId}; Path=/; Max-Age=31536000; SameSite=Lax`
53+
window.parent.document.cookie = `${storageKey}=${deviceId}; Path=/; Max-Age=31536000; SameSite=Lax`;
5354
}
5455
}
5556
}

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
import Ingestly from './ingestly';
22
const nameSpace = 'Ingestly';
3-
window[nameSpace] = new Ingestly();
3+
window[nameSpace] = new Ingestly();

0 commit comments

Comments
 (0)