Learn business growth with Google Analytics 4 › Forums › Google Analytics 4 › Troubleshooting Custom JavaScript Variables in Google Tag Manager › Reply To: Troubleshooting Custom JavaScript Variables in Google Tag Manager
-
The issue comes from the fact that you’re trying to use regex on the wrong kind of object. The document.body is an HTML object, not a string. If you want to scan the body of your HTML for a specific word, you need to convert the body to a string first. You can use the
innerHTML
property to achieve this. Your function would then look like this:`javascript
function() {
var bodyContent = document.body.innerHTML;
var result = /stellar/.test(bodyContent);
return result;
}
`
So with this update, you are checking if the word “stellar” is anywhere within the HTML of your body, not within the body object itself.