Created
November 18, 2015 01:46
-
-
Save smhmic/7db28007f070b3008ac8 to your computer and use it in GitHub Desktop.
GTM - dynamic dataLayer Name
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> | |
var dataLayer = window[{{dataLayer Name}}]; | |
dataLayer.push({ 'event' : 'test' }) | |
</script> |
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
function(){ | |
var k, timestamp; | |
timestamp = google_tag_manager[{{Container ID}}].dataLayer.get('gtm.start'); | |
for( k in window ){ | |
if( window[k] && window[k][0] && timestamp === window[k][0]['gtm.start'] ){ | |
return k; | |
} | |
} | |
} |
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
function(){ | |
var i, src, matches, scripts, cacheObj; | |
scripts = document.getElementsByTagName( "script" ); | |
for( i in scripts ){ | |
src = scripts[i].getAttribute( 'src' ); | |
if( /^(?:https?:)?\/\/www\.googletagmanager\.com\/gtm\.js\?(?:.*&)?id={{Container ID}}(?:&|$)/.exec( src ) ){ | |
matches = /[?&]l=([^&]+)(?:&|$)/.exec( src ); | |
return matches ? matches[1] : 'dataLayer'; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment