Skip to content

Commit

Permalink
Added button and links click event tracking.
Browse files Browse the repository at this point in the history
Added JS errors tracking.
  • Loading branch information
Samuel Imolorhe committed Nov 8, 2017
1 parent 2bdbaaa commit dfbe254
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
16 changes: 16 additions & 0 deletions src/app/utils/events.ts
@@ -0,0 +1,16 @@

/**
* Event delegation
* @param eventName
* @param elSelector
* @param fn
* @example on('click', '.el', fn);
*/
export const on = (eventName, elSelector, fn) => {
document.body.addEventListener(eventName, function(e) {
if (e.target && e.target.matches(elSelector)) {
console.log('Fired.', e);
fn(e);
}
});
};
26 changes: 18 additions & 8 deletions src/app/utils/ga.ts
@@ -1,16 +1,22 @@
import config from '../config';
import { on } from './events';

// Track button click event
export const trackButton = e => {
window['_gaq'].push(['_trackEvent', e.target.id, 'clicked']);
window['_gaq'].push(['_trackEvent', `${e.target.innerText} (${e.target.className})`, 'clicked']);
};

// Add event listener to all buttons
export const initTrackButtons = () => {
const buttons = document.querySelectorAll('button');
for (let i = 0; i < buttons.length; i++) {
buttons[i].addEventListener('click', trackButton);
}
// 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
Expand All @@ -27,5 +33,9 @@ export const initTracking = () => {
const s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(ga, s);

initTrackButtons();
// Listen for click events on buttons and links
on('click', 'button', trackButton);
on('click', 'a', trackButton);

trackJSErrors();
};

0 comments on commit dfbe254

Please sign in to comment.