-
-
Notifications
You must be signed in to change notification settings - Fork 160
/
google-analytics.js
123 lines (97 loc) · 3.26 KB
/
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
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
import { isPresent } from '@ember/utils';
import { assert } from '@ember/debug';
import { capitalize } from '@ember/string';
import { compact } from 'ember-metrics/-private/utils/object-transforms';
import removeFromDOM from 'ember-metrics/-private/utils/remove-from-dom';
import BaseAdapter from './base';
export default class GoogleAnalytics extends BaseAdapter {
gaSendKey = 'send';
toStringExtension() {
return 'GoogleAnalytics';
}
install() {
const config = { ...this.config };
const { id, sendHitTask, trace, require, debug, trackerName } = config;
if (trackerName) {
this.gaSendKey = `${trackerName}.send`;
}
assert(
`[ember-metrics] You must pass a valid \`id\` to the ${this.toString()} adapter`,
id
);
delete config.id;
delete config.require;
delete config.debug;
delete config.sendHitTask;
delete config.trace;
delete config.trackerName;
const hasOptions = isPresent(Object.keys(config));
this._injectScript(debug);
if (trace === true) {
window.ga_debug = { trace: true };
}
window.ga('create', id, hasOptions ? config : 'auto', trackerName);
if (require) {
require.forEach((plugin) => {
window.ga('require', plugin);
});
}
if (sendHitTask === false) {
window.ga('set', 'sendHitTask', null);
}
}
/* eslint-disable */
// prettier-ignore
_injectScript(debug) {
(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)
})(window,document,'script',`https://www.google-analytics.com/analytics${debug ? '_debug' : ''}.js`,'ga');
}
/* eslint-enable */
identify(options = {}) {
const compactedOptions = compact(options);
const { distinctId } = compactedOptions;
window.ga('set', 'userId', distinctId);
}
trackEvent(options = {}) {
const compactedOptions = compact(options);
const sendEvent = { hitType: 'event' };
const eventKeys = ['category', 'action', 'label', 'value'];
let gaEvent = {};
if (compactedOptions.nonInteraction) {
gaEvent.nonInteraction = compactedOptions.nonInteraction;
delete compactedOptions.nonInteraction;
}
for (let key in compactedOptions) {
if (eventKeys.includes(key)) {
const capitalizedKey = capitalize(key);
gaEvent[`event${capitalizedKey}`] = compactedOptions[key];
} else {
gaEvent[key] = compactedOptions[key];
}
}
const event = { ...sendEvent, ...gaEvent };
const gaSendKey = this.gaSendKey;
window.ga(gaSendKey, event);
return event;
}
trackPage(options = {}) {
const compactedOptions = compact(options);
const sendEvent = { hitType: 'pageview' };
const event = { ...sendEvent, ...compactedOptions };
for (let key in compactedOptions) {
// eslint-disable-next-line
if (compactedOptions.hasOwnProperty(key)) {
window.ga('set', key, compactedOptions[key]);
}
}
const gaSendKey = this.gaSendKey;
window.ga(gaSendKey, event);
return event;
}
uninstall() {
removeFromDOM('script[src*="google-analytics"]');
delete window.ga;
}
}