JavaScript in GTM: Good or Bad?
how to avoid JS + how to use it
Markus Baersch
Avoiding JS
You might not need JS!
Avoid JS: Facebook via template - ecom with one tag
● Facebook Tag Template:
github.com/jabjabonlinemarketing/
FacebookPixel-by-JabJab
Avoid JS: Use community templates whenever you can (even if they might be slower)
Avoiding JS
use standard elements
JS… because: why not?
...should be a standard regex variable
Build!
markus-baersch.de/gtmbook
Avoid JS: localStorage / sessionStorage
JS as a Tool
JS in console? Okay!
Test CSS selectors in console…
push events
and data…
…debug stuff
JS as a Tool
JS in console? Okay!
Test code in console with {{variables}}?
https://gist.github.com/mbaersch/
JS as a Tool
JS in console? Okay!
Test code in console with {{variables}}?
https://gist.github.com/mbaersch/
Using JS
… in JS variables
Return a function…
or several functions in a “Lib” variable!
16 / 32 ;)
Using JS
… in JS variables
Using functions from “Lib”
Using JS
… in JS variables
Minimize calls / cache results!
Use long & descriptive names and / or use own
namespace in global scope / persistence!
Using JS
… in JS variables Access dataLayer directly (if no other
references for a dataLayer variable)
Logging? Only in debug mode
Using JS
Useless knowledge(?)
Push to dataLayer without changing the
actual JS object
… but in GTM (after the next event):
A smart way for sharing sensible
data “only” with your GTM?
Hide personal data?
Using JS
… in HTML tags
IIFE
“Immediately Invoked
Function Expression”
Using JS
GTM vs ES6
Most cases: just convert ES6
ES6 errors: 90+% are just const, let, and arrow functions
Using JS
Even ChatGPT can do it
but TEST, TEST, TEST!
ES6 errors: … if it is a tag and you cannot avoid ES6…
ES6 errors: … add a “text/gtmscript” attribute
… but remember: skipping validation means: a broken site is 100% on you ;)
Done ;)
Thank You!
markus-baersch.de/slideshare
markus-baersch.de/podcast
markus-baersch.de/youtube

JavaScript - Good or Bad? Tips for Google Tag Manager