Skip to content

Instantly share code, notes, and snippets.

@MatthewDaniels
Last active July 18, 2016 05:57
Show Gist options
  • Save MatthewDaniels/df0d8ae0ebbcbb7cfe05f500cb41577e to your computer and use it in GitHub Desktop.
Save MatthewDaniels/df0d8ae0ebbcbb7cfe05f500cb41577e to your computer and use it in GitHub Desktop.
ga(function(tracker) {
// Grab a reference to the default sendHitTask function.
var originalSendHitTask = tracker.get('sendHitTask');
// Modifies sendHitTask to send a copy of the request to a local server after
// sending the normal request to www.google-analytics.com/collect.
tracker.set('sendHitTask', function(model) {
// sends the original hit task
originalSendHitTask(model);
var query = model.get('hitPayload'),
toType = function(a) {
return ({}).toString.call(a).match(/\s([a-zA-Z]+)/)[1].toLowerCase();
}, map = {};
// map the hit query into a proper object
query.replace(/([^&=]+)=?([^&]*)(?:&+|$)/g, function(match, key, value) {
if (key in map) {
// the key already exists, so we need to check if it is an array, if not, make it an array and add the new value
if (toType(map[key]) !== 'array') {
// it's not an array - make it an array
map[key] = [map[key]];
}
// push the new value into the array
map[key].push(value);
} else {
// put the value into the map
map[key] = value;
}
});
console.log('0:', map);
console.log('1:', model.get('hitPayload'));
console.log('2:', model);
console.log('3:', map['dp']);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment