-
Notifications
You must be signed in to change notification settings - Fork 121
/
Copy pathsetup.js
67 lines (60 loc) · 2.62 KB
/
setup.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// update the cookie if it exists, if it doesn't, create a new one, lasting 2 years
Cookie.exists('uid') ? Cookie.set('uid', Cookie.get('uid'), 2*365*24*60) : Cookie.set('uid', Helper.guid(), 2*365*24*60);
// save any utms through as session cookies
Cookie.setUtms();
// process the queue and future incoming commands
pixelFunc.process = function(method, value, optional) {
if (method === 'init') {
Config.id = value;
} else if(method === 'param') {
Config.params[value] = () => optional
} else if(method === 'event') {
if(value === 'pageload' && !Config.pageLoadOnce) {
Config.pageLoadOnce = true;
new Pixel(value, pixelFunc.t, optional);
} else if(value !== 'pageload' && value !== 'pageclose') {
new Pixel(value, Helper.now(), optional);
}
}
}
// run the queued calls from the snippet to be processed
for (var i = 0, l = pixelFunc.queue.length; i < l; i++) {
pixelFunc.process.apply(pixelFunc, pixelFunc.queue[i]);
}
// https://github.com/GoogleChromeLabs/page-lifecycle/blob/master/src/Lifecycle.mjs
// Safari does not reliably fire the `pagehide` or `visibilitychange`
var isSafari = typeof safari === 'object' && safari.pushNotification;
var isPageHideSupported = 'onpageshow' in self;
// IE9-10 do not support the pagehide event, so we fall back to unload
// pagehide event is more reliable but less broad than unload event for mobile and modern browsers
var pageCloseEvent = isPageHideSupported && !isSafari ? 'pagehide' : 'unload';
window.addEventListener(pageCloseEvent, function() {
if (!Config.pageCloseOnce) {
Config.pageCloseOnce = true;
new Pixel('pageclose', Helper.now(), function() {
// if a link was clicked in the last 5 seconds that goes to an external host, pass it through as event data
if (Helper.isPresent(Config.externalHost) && (Helper.now() - Config.externalHost.time) < 5*1000) {
return Config.externalHost.link;
}
});
}
});
window.onload = function() {
var aTags = document.getElementsByTagName('a');
for (var i = 0, l = aTags.length; i < l; i++) {
aTags[i].addEventListener('click', function(_e) {
if (Url.externalHost(this)) {
Config.externalHost = { link: this.href, time: Helper.now() };
}
}.bind(aTags[i]));
}
var dataAttributes = document.querySelectorAll('[data-OPIX_FUNC-event]')
for (var i = 0, l = dataAttributes.length; i < l; i++) {
dataAttributes[i].addEventListener('click', function(_e) {
var event = this.getAttribute('data-OPIX_FUNC-event');
if (event) {
new Pixel(event, Helper.now(), this.getAttribute('data-OPIX_FUNC-data'));
}
}.bind(dataAttributes[i]));
}
}