Skip to content

Instantly share code, notes, and snippets.

@josfaber
Last active November 20, 2021 15:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save josfaber/fb86a2c4bef4a20df8c7f5df8859abb0 to your computer and use it in GitHub Desktop.
Save josfaber/fb86a2c4bef4a20df8c7f5df8859abb0 to your computer and use it in GitHub Desktop.
GTM Google Tagmanager implementation examples
<!-- IN HEAD -->
<!-- Google Tag Manager -->
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','{{ GTM_ID }}');</script>
<!-- End Google Tag Manager -->
<!-- IN BODY -->
<!-- Google Tag Manager (noscript) -->
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id={{ GTM_ID }}"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<!-- End Google Tag Manager (noscript) -->
<script>
// IN JS: normal event push example
window.dataLayer.push( {
'event': 'settings',
'action': 'cookie settings',
'label': 'accept'
} );
</script>
<script>
// IN JS: event push with ecommerce object example
window.dataLayer.push( {
'event': 'forms',
'action': 'form submit',
'label': 'application form',
'ecommerce': {
'purchase': {
'actionField': {
'id': window.vacancy_no,
'revenue': '1.00',
},
'products': [ {
'name': window.vacancy_title,
'quantity': 1,
} ]
}
}
} );
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment