-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathga_cid.js
executable file
·54 lines (48 loc) · 1.36 KB
/
ga_cid.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
/**
* Set Google Analytics Client Id in cooke and
* widow object to pass it with form post either get
* it form javascript
*/
function setGaCid() {
if(typeof ga == "undefined") {
console.warn('package @mae/gacid object ga is not defined.. include google analytics to get ga_client_id');
return;
}
ga(function(tracker) {
var clientId = '';
clientId = tracker.get('clientId');
if(clientId == '') {
clientId = ga.getAll()[0].get('clientId');
}
window.ga_client_id = clientId;
document.cookie = clientId;
console.log('ga client ' + tracker.get('clientId'));
});
}
/**
* Check Google Analytics Loaded
* retry 5 times
*/
var counter = 1;
function checkIfAnalyticsLoaded() {
if (window.ga) {
setGaCid();
} else {
//this counter set stack for this setTimeout
counter = counter + 1;
if (counter < 6){
setTimeout(() => {
if(typeof checkIfAnalyticsLoaded == 'function') {
checkIfAnalyticsLoaded()
}
else {
console.warn('package @mae/gacid function checkIfAnalyticsLoaded is not defined!');
}
}, 500);
} else {
setGaCid();
return;
}
}
}
window.onload = checkIfAnalyticsLoaded();