-
Notifications
You must be signed in to change notification settings - Fork 0
/
gat.js
67 lines (53 loc) · 2.4 KB
/
gat.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
function GA2AT(tracker, config) {
var site = config.site || null;
var log = config.log || 'logp.xiti.com';
var pixel = config.pixel || 'hit.xiti';
var useTitle = config.useTitle || false;
ga(function (tracker) {
var originalSendHitTask = tracker.get('sendHitTask');
tracker.set('sendHitTask', function (model) {
originalSendHitTask(model);
var hitParams = null;
if (site) {
var pageLabel = '';
if(useTitle) { pageLabel = tracker.get('title'); }
var hitType = tracker.get('hitType');
if (hitType === 'pageview') {
hitParams = '&p=' + encodeURIComponent(pageLabel);
}
if (hitType === 'social') {
var socialNetwork = tracker.get('socialNetwork') || '';
var socialAction = tracker.get('socialAction') || '';
var socialTarget = tracker.get('socialTarget') || '';
hitParams = '&p=social::' + encodeURIComponent(socialNetwork) + '::' + encodeURIComponent(socialAction) + '::' + encodeURIComponent(socialTarget) + '&click=a&type=click&pclick=' + encodeURIComponent(pageLabel);
}
if (hitType === 'event') {
var eventCategory = tracker.get('eventCategory') || '';
var eventAction = tracker.get('eventAction') || '';
var eventLabel = tracker.get('eventLabel') || '';
var eventValue = tracker.get('eventValue') || '';
hitParams = '&p=' + encodeURIComponent(eventCategory) + '::' + encodeURIComponent(eventAction) + '::' + encodeURIComponent(eventLabel) + '::' + encodeURIComponent(eventValue) + '&click=a&type=click&pclick=' + encodeURIComponent(pageLabel);
}
if (hitParams) {
var idclient = tracker.get('clientId');
var ref = tracker.get('referrer') || '';
var base = 'https://' + log + '/' + pixel + '?s=' + site + '&idclient=' + idclient;
var suffix = '&ref=' + ref;
var hit = base + hitParams + suffix
if (navigator.sendBeacon) {
navigator.sendBeacon(hit);
} else {
var xhr = new XMLHttpRequest();
xhr.open('GET', hit, true);
xhr.send(null);
}
}
}
});
});
}
function providePlugin(pluginName, pluginvarructor) {
var ga = window[window['GoogleAnalyticsObject'] || 'ga'];
if (typeof ga == 'function') { ga('provide', pluginName, pluginvarructor); }
}
providePlugin('GA2AT', GA2AT);