-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy pathpixel.js
79 lines (72 loc) · 3.08 KB
/
pixel.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
68
69
70
71
72
73
74
75
76
77
78
79
class Pixel {
constructor(event, timestamp, optional) {
this.params = [];
this.event = event;
this.timestamp = timestamp;
this.optional = Helper.optionalData(optional);
this.buildParams();
this.send();
}
buildParams() {
const attr = this.getAttribute();
for (var index in attr) {
if (attr.hasOwnProperty(index)) {
this.setParam(index, attr[index](index));
}
}
}
getAttribute() {
return {
id: () => Config.id, // website Id
uid: () => Cookie.get('uid'), // user Id
ev: () => this.event, // event being triggered
ed: () => this.optional, // any event data to pass along
v: () => Config.version, // openpixel.js version
dl: () => window.location.href, // document location
rl: () => document.referrer, // referrer location
ts: () => this.timestamp, // timestamp when event was triggered
de: () => document.characterSet, // document encoding
sr: () => window.screen.width + 'x' + window.screen.height, // screen resolution
vp: () => window.innerWidth + 'x' + window.innerHeight, // viewport size
cd: () => window.screen.colorDepth, // color depth
dt: () => document.title, // document title
bn: () => Browser.nameAndVersion(), // browser name and version number
md: () => Browser.isMobile(), // is a mobile device?
ua: () => Browser.userAgent(), // user agent
tz: () => (new Date()).getTimezoneOffset(), // timezone
utm_source: key => Cookie.getUtm(key), // get the utm source
utm_medium: key => Cookie.getUtm(key), // get the utm medium
utm_term: key => Cookie.getUtm(key), // get the utm term
utm_content: key => Cookie.getUtm(key), // get the utm content
utm_campaign: key => Cookie.getUtm(key), // get the utm campaign
utm_source_platform: key => Cookie.getUtm(key), // get the utm source platform
utm_creative_format: key => Cookie.getUtm(key), // get the utm creative format
utm_marketing_tactic: key => Cookie.getUtm(key), // get the utm marketing tactic
...Config.params
}
}
setParam(key, val) {
if (Helper.isPresent(val)) {
this.params.push(`${key}=${encodeURIComponent(val)}`);
} else {
this.params.push(`${key}=`);
}
}
send() {
window.navigator.sendBeacon ? this.sendBeacon() : this.sendImage();
}
sendBeacon() {
window.navigator.sendBeacon(this.getSourceUrl());
}
sendImage() {
this.img = document.createElement('img');
this.img.src = this.getSourceUrl();
this.img.style.display = 'none';
this.img.width = '1';
this.img.height = '1';
document.getElementsByTagName('body')[0].appendChild(this.img);
}
getSourceUrl() {
return `${pixelEndpoint}?${this.params.join('&')}`;
}
}