-
-
Notifications
You must be signed in to change notification settings - Fork 320
/
ga.ts
41 lines (34 loc) · 1.05 KB
/
ga.ts
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
import config from '../config';
import { on } from './events';
// Track button click event
export const trackButton = e => {
window['_gaq'].push(['_trackEvent', `${e.target.innerText} (${e.target.className})`, 'clicked']);
};
// Track JavaScript errors
export const trackJSErrors = () => {
window.addEventListener('error', (e: any) => {
window['_gaq'].push([
'_trackEvent',
'JS Error',
e.message,
e.filename + ': ' + e.lineno,
true
]);
});
};
// Initialise tracking
export const initTracking = () => {
window['_gaq'] = window['_gaq'] || [];
window['_gaq'].push(['_setAccount', config.ga]);
window['_gaq'].push(['_trackPageview']);
const ga = document.createElement('script');
ga.type = 'text/javascript';
ga.async = true;
ga.src = 'https://ssl.google-analytics.com/ga.js';
const s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(ga, s);
// Listen for click events on buttons and links
on('click', 'button', trackButton);
on('click', 'a', trackButton);
trackJSErrors();
};