Skip to content

Instantly share code, notes, and snippets.

@chrisblakley
Last active November 7, 2015 18:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisblakley/f433b28a741799c4239c to your computer and use it in GitHub Desktop.
Save chrisblakley/f433b28a741799c4239c to your computer and use it in GitHub Desktop.
Reduce the amount of speculation when making insights in Google Analytics by using context clues to determine the event intent from user interactions. https://gearside.com/event-intent-using-context-clues-determine-user-intention/
jQuery('a.some-link').on('mousedown tap touch', function(e){
eventIntent = ( e.which >= 2 )? 'Intent' : 'Explicit';
ga('set', 'dimension3', eventIntent);
ga('send', 'event', 'Category', 'Action', 'Label', 'Value');
});
jQuery(document).on('cut copy', function(){
var selection = window.getSelection() + '';
var emailPattern = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
var phonePattern = /^(?:(?:\+?1\s*(?:[.-]\s*)?)?(?:\(\s*([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9])\s*\)|([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))\s*(?:[.-]\s*)?)?([2-9]1[02-9]|[2-9][02-9]1|[2-9][02-9]{2})\s*(?:[.-]\s*)?([0-9]{4})(?:\s*(?:#|x\.?|ext\.?|extension)\s*(\d+))?$/;
if ( emailPattern.test(selection) ){ //Mailto link
ga('set', 'dimension3', 'Intent');
} else if ( phonePattern.test(selection) ){ //Phone number
ga('set', 'dimension3', 'Intent');
}
ga('send', 'event', 'Category', 'Action', 'Label', 'Value');
});
window.matchMedia('print').addListener(function(mq){ //IE9+
if ( !mq.matches ){
ga('set', 'dimension3', 'Intent');
ga('send', 'event', 'Category', 'Action', 'Label', 'Value');
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment