-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrackjs.js
210 lines (203 loc) · 7.18 KB
/
trackjs.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
var trackHQ;
var debug = false;
var clientID; // This is the ID for TrackJS
var appUserID; // This is an app-supplied user ID, useful for cross-correlating TrackJS events with other logging
var geoPromise;
var geo;
var pageLoadTime = Date.now();
var jQuerySrcLocation = 'http://code.jquery.com/jquery-1.11.0.min.js';
var track = {
init: function(configTrackHQ, configAppUserID, configDebug) {
trackHQ=configTrackHQ;
appUserID=configAppUserID||null;
debug=configDebug||false;
clientID = this.getClientID();
this.logger('Set ClientID');
this.logger('TrackJS Loading...');
if (typeof jQuery == 'undefined') {
this.logger('Loading jQuery');
var jQueryScript = document.createElement('script');
jQueryScript.type = 'text/javascript';
jQueryScript.src = jQuerySrcLocation;
document.getElementsByTagName('head')[0].appendChild(jQueryScript);
setTimeout(function() {
track.attachEvents();
track.startViewTimers();
}, 100);
setTimeout(function() {
geoPromise = track.getGeo();
geoPromise.then(coords => track.logger({"comment": "got geo", "geo": coords}));
}, 5000);
}
},
getClientID: function() {
if ( this.getCookie("TrackJS") )
{
this.logger("Found TrackJS cookie");
return this.getCookie("TrackJS");
}
else
{
this.logger("No TrackJS cookie, generating new ID")
var newClientId = this.generateGUID();
this.setCookie("TrackJS", newClientId, 365);
return newClientId;
}
},
getGeo: function() {
return new Promise((resolve, reject) =>
navigator.permissions ?
// Permission API is implemented
navigator.permissions.query({name:'geolocation'}).then(permission =>
// is geolocation granted?
permission.state === "granted"
? navigator.geolocation.getCurrentPosition(pos => resolve(pos.coords))
: resolve(null),
reject) :
// Permission API was not implemented
reject(new Error("Permission API is not supported"))
)
},
attachEvents: function() {
if (typeof jQuery == 'undefined') {
this.logger('jQuery not loaded, waiting...');
setTimeout(function() { track.attachEvents() }, 500);
}
else
{
this.logger('jQuery loaded');
this.logger('Attaching events');
$(document).ready( function() { track.sendEvent({"event": "load"}) } );
$(window).on('beforeunload', function() {
track.sendEvent({"event": "unload"});
});
$(document).on("click", function(event) {
track.sendEvent({"event": "click", "click": {"x": event.pageX, "y": event.pageY}, "scrolled": {"x": $(document).scrollLeft(), "y": $(document).scrollTop()}});
} );
$(document).on("visibilitychange", function() {
track.sendEvent({"event": "visibilityChange", "visibility": document.visibilityState});
});
/*
* Add additional events here, for example you could monitor mouseovers using this:
*
$("body").find("*").each(function() {
$(this).mouseover(function() {
track.sendEvent({event: 'mouseOver', data: this.toString(), tag: this.tagName, name: this.name, id: this.id})
})
});
$("body").find("*").each(function() {
$(this).mouseout(function() {
track.sendEvent({event: 'mouseOut', data: this.toString(), tag: this.tagName, name: this.name, id: this.id})
})
});
$("a").each(function() {
$(this).click(function() {
track.sendEvent({event: 'click', data: this.toString(), tag: this.tagName, name: this.name, id: this.id})
})
});
$("body").find("*").each(function() {
$(this).click(function() {
track.sendEvent({event: 'click', data: this.toString(), tag: this.tagName, name: this.name, id: this.id});
});
});
*/
}
},
startViewTimers: function() {
setTimeout(function(){
track.sendEvent({event: "pageViewTimer", "pageViewTimer": {"timer": 10, "visibility": document.visibilityState} });
}, 10000); // 0-10 seconds
setTimeout(function(){
track.sendEvent({event: "pageViewTimer", "pageViewTimer": {"timer": 30, "visibility": document.visibilityState} });
}, 30000); // 10-30 seconds
setTimeout(function(){
track.sendEvent({event: "pageViewTimer", "pageViewTimer": {"timer": 60, "visibility": document.visibilityState} });
}, 60000); // 30-60 seconds
setTimeout(function(){
track.sendEvent({event: "pageViewTimer", "pageViewTimer": {"timer": 120, "visibility": document.visibilityState} });
}, 120000); // 1-2 minutes
setTimeout(function(){
track.sendEvent({event: "pageViewTimer", "pageViewTimer": {"timer": 300, "visibility": document.visibilityState} });
}, 300000); // 2-5 minutes
setTimeout(function(){
track.sendEvent({event: "pageViewTimer", "pageViewTimer": {"timer": 600, "visibility": document.visibilityState} });
}, 600000); // 5-10 minutes
setTimeout(function(){
track.sendEvent({event: "pageViewTimer", "pageViewTimer": {"timer": 900, "visibility": document.visibilityState} });
}, 900000); // 10-15 minutes
setTimeout(function(){
track.sendEvent({event: "pageViewTimer", "pageViewTimer": {"timer": 1800, "visibility": document.visibilityState} });
}, 1800000); // 15-30 minutes
},
sendEvent: function(event) {
event._time = Date.now();
event._elapsed = event._time - pageLoadTime;
event._url = document.URL;
event._title = document.title;
event._clientid = clientID;
event._appUserID = appUserID;
if ( geo ) {
console.log("Using pre-set geo: " + geo);
event._geo = geo;
} else {
console.log("No Geo set, trying to find it...");
track.getGeo().then(coords => track.logger(coords));
}
/*
* AJAX-style callback to server (note CORS)
*/
/*
$.ajax({
type: "POST",
crossDomain: true,
url: trackHQ,
data: event
}).done(this.logger(event));
*/
/*
* Pixel-style callback
*/
var pixel = document.createElement('img');
pixel.id = track.generateGUID()
pixel.src = trackHQ + "?event=" + btoa(JSON.stringify(event));
document.getElementsByTagName('body')[0].appendChild(pixel);
setTimeout(function(){
pixelToRemove = document.getElementById(pixel.id);
pixelToRemove.parentNode.removeChild(pixelToRemove);
}, 1000)
this.logger(event);
},
logger: function(logMsg) {
if ( debug ) {
// console.log('track.js: ' + clientID + " : " + JSON.stringify(logMsg));
console.log(JSON.stringify(logMsg));
}
},
generateGUID: function() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
return v.toString(16);
});
},
getCookie: function(cookieName) {
var name = cookieName + "=";
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.split(';');
for(var i = 0; i <ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return false;
},
setCookie: function(cookieName, cookieValue, cookieValidityDays) {
var d = new Date();
d.setTime(d.getTime() + (cookieValidityDays * 24 * 60 * 60 * 1000));
var expires = "expires="+d.toUTCString();
document.cookie = cookieName + "=" + cookieValue + ";" + expires + ";path=/";
}
};