-
Notifications
You must be signed in to change notification settings - Fork 14
/
ember-google-analytics.js
49 lines (41 loc) · 1.27 KB
/
ember-google-analytics.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
Ember.GoogleAnalyticsTrackingMixin = Ember.Mixin.create({
pageHasGa: function() {
return window.ga && typeof window.ga === "function";
},
logTrackingEnabled: function() {
return !!window.ENV && !!window.ENV.LOG_EVENT_TRACKING;
},
logTracking: function() {
Ember.Logger.info('Tracking Google Analytics event: ', arguments);
},
trackPageView: function(page) {
if (this.pageHasGa()) {
if (!page) {
var loc = window.location;
page = loc.hash ? loc.hash.substring(1) : loc.pathname + loc.search;
}
ga('send', 'pageview', page);
}
if (this.logTrackingEnabled()) {
this.logTracking('pageview', page);
}
},
trackEvent: function(category, action, label, value) {
if (this.pageHasGa()) {
ga('send', 'event', category, action, label, value);
}
if (this.logTrackingEnabled()) {
this.logTracking('event', category, action, label, value);
}
}
});
Ember.Application.initializer({
name: "googleAnalytics",
initialize: function(container, application) {
var router = container.lookup('router:main');
router.on('didTransition', function() {
this.trackPageView(router.rootURL.slice(0, -1) + this.get('url'));
});
}
});
Ember.Router.reopen(Ember.GoogleAnalyticsTrackingMixin);