Last active
July 18, 2017 19:07
-
-
Save postman31/af377780e35d07afbe1c60428940459e to your computer and use it in GitHub Desktop.
UA plugins with GTM
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
<script> | |
(function(i,s,o,g,r,a,m){ // Modified UA function to pre-set 'ga' queue forming. | |
var ga2fb = function(tracker) { // plugin constructor | |
this.tracker = tracker; | |
console.log('hello from constructor'); | |
// Copy the original hit dispatch function | |
var originalSendHitTask = this.tracker.get('sendHitTask'); | |
// Modify the existing hit dispatcher to log a copy of the hit | |
this.tracker.set('sendHitTask', function(model) { | |
originalSendHitTask(model); // Send the original hit as usual | |
console.log(model.get('hitPayload')); | |
}); | |
}; | |
i['GoogleAnalyticsObject']= r; | |
if (!i[r]) { // check if actual 'ga' oject is not instatinated | |
i[r]= function() { | |
var queue = (i[r].q=i[r].q||[]); // modifying que | |
queue.push(arguments); | |
var args = [].slice.call(arguments); | |
if (arguments[0] == 'create') { | |
var argumentor = function () { | |
return arguments; | |
}; | |
var trackerName = args.find(function(elm) {return elm.hasOwnProperty('name')}).name; | |
queue.push(argumentor((trackerName + '.require'), 'ga2fb')); | |
queue.push(argumentor('provide', 'ga2fb', ga2fb)); | |
console.log('hello from hack. got "create" in pushes'); | |
console.log(queue); | |
} | |
}, i[r].l=1*new Date(); | |
} else { // resetting 'create' method for existing ga object to call 'require' after set | |
var originalGaCreate = i[r].create; | |
i[r].create = function () { | |
originalGaCreate.apply(this, arguments); | |
var args = [].slice.apply(arguments); | |
var trackerName = args.find(function(elm) {return elm.hasOwnProperty('name')}).name; | |
var self = this; | |
self(trackerName + '.require', 'ga2fb'); | |
} | |
} | |
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga'); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment