Last active
July 18, 2016 05:57
-
-
Save MatthewDaniels/df0d8ae0ebbcbb7cfe05f500cb41577e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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