Last active
May 5, 2016 17:34
-
-
Save smhmic/804673afd359791cd2467bd8945aed23 to your computer and use it in GitHub Desktop.
Lets you use data layer pushes with no 'event' for a GTM Trigger.
This file contains hidden or 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> | |
/** | |
* Data layer pushes should always have an event (even in simple <head> snippets). | |
* But if you're dealing with an event-less push upon which you need to be able | |
* to fire a Trigger, and you cannot change the push snippet, this code will | |
* let you detect the push and add an event dynamically. | |
* | |
* Tested in Chrome, Firefox, and IE9. | |
*/ | |
(function(){ | |
var dataLayer = window.dataLayer, origPushFn = dataLayer.push; | |
dataLayer.push = function(){ | |
var i, obj, args = [].slice.call(arguments,0); | |
try{ | |
for( i=0; i<args.length; i++ ){ | |
if( !( obj=args[i] ) || obj.event ) continue; | |
// v v v CUSTOM CODE HERE v v v | |
/* | |
At this point, `obj` is data that was pushed without an 'event' value. | |
Test `obj` for conditions of the push on which you want base a Trigger, | |
and set `obj.event`. | |
EXAMPLE: | |
if( obj.userId ){ | |
obj.event = 'provideUserId'; | |
} | |
*/ | |
// ^ ^ ^ CUSTOM CODE HERE ^ ^ ^ | |
} | |
}catch(ex){} | |
return origPushFn.apply( dataLayer, args ); | |
}; | |
})() | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment