-
Notifications
You must be signed in to change notification settings - Fork 29
/
ganalytics.js
74 lines (55 loc) · 2.11 KB
/
ganalytics.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
GAnalytics = {}
GAnalytics.pageview = function(pageLocation) {
console.log("Analytics code is not loaded yet.");
};
GAnalytics.event = function(category, action, label, value) {
console.log("Analytics code is not loaded yet.");
};
load = function(i,s,o,g,r,a,m) {
i['GoogleAnalyticsObject']=r;
i[r]=i[r] || function(){
(i[r].q=i[r].q||[]).push(arguments)}
,i[r].l=1*new Date();
a=s.createElement(o), m=s.getElementsByTagName(o)[0];
a.async=1;
a.src=g;
m.parentNode.insertBefore(a,m)
};
if(Meteor.settings && Meteor.settings.public !== undefined && Meteor.settings.public.ga !== undefined && Meteor.settings.public.ga.account !== undefined) {
load(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
var gaSettings = Meteor.settings.public.ga,
gaConfig = {};
// cookie settings
if(typeof gaSettings.cookieName !== 'undefined')
gaConfig.cookieName = gaSettings.cookieName;
if(typeof gaSettings.cookieDomain !== 'undefined')
gaConfig.cookieDomain = gaSettings.cookieDomain;
if(typeof gaSettings.cookieExpires !== 'undefined')
gaConfig.cookieExpires = gaSettings.cookieExpires;
// if gaConfig is still empty, default it to 'auto'
if(Object.keys(gaConfig).length === 0)
gaConfig = 'auto';
ga('create', gaSettings.account, gaConfig);
if(gaSettings.anonymizeIp) {
ga('set', 'anonymizeIp', true);
}
if (gaSettings.trackInterests)
ga('require', 'displayfeatures');
if (gaSettings.trackInPage)
ga('require', 'linkid', 'linkid.js');
GAnalytics.pageview = function(pageLocation) {
if(!!gaSettings.debug)
console.log("Logging pageview: "+pageLocation)
if(!pageLocation) {
pageLocation = window.location.pathname;
}
ga('send', 'pageview', pageLocation);
}
GAnalytics.event = function(category, action, label, value) {
if(!!gaSettings.debug)
console.log("Logging event: "+category+" | "+ action + " | " + label + " | " + value)
ga('send', 'event', category, action, label, value);
}
} else {
console.log("public.ga.account has not been set in your settings.json file.");
}