I'm attempting to write a Custom JavaScript variable in Google Tag Manager. I want to see if a certain word appears in the body section of pages' code in order to distinguish subcategories. So far I have:
function(){
{var hasStellar = (document.body);
var result = /stellar/.test(hasStellar);
return result}}
with document.body being:
".project-template-template-stellar-project.project-template-viewstem" +
"plate-stellar-project.single.single-project.postid-20597.--nav-trans" +
"parent.app-data.index-data.singular-data.s" +
"ingle-data.single-project-data.single-project-data.template-stellar-project-data"
When I preview the Custom JavaScript Variable, it returns false. If anyone knows how to fix this, or a better way to do this, I'd appreciate it!
document.body is not a string. Here, try this out: document.body.outerHTML
instead.
Also make sure that your GTM isn't being blocked by CSP. Check the console for errors.
Finally, make sure the time at which you check the value of the variable is after the body is properly populated.