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

  • Nam

    17 June 2023 at 9:16 pm

    Sure, let me break it down for you. The function you’ve written is checking if the word ‘stellar’ is in your webpage’s code, right? But here’s the thing: you’re treating ‘document.body’ as a string, and it’s not. It’s actually an HTML object. So, instead of ‘document.body’, you’d want to use ‘document.body.outerHTML’. This gives you the HTML content of the body element as a string, and you can do your search in that.

    But that’s not all. Apparently there’s something called CSP (Content Security Policy) that can block your Google Tag Manager. So you might want to check if that’s happening. Simply look at your console for any error messages, and if you see something related to CSP, there’s your culprit.

    Lastly, the timing when you’re checking this function matters. If the body of your webpage isn’t fully loaded by the time your function is checking for the word ‘stellar’, it might not find it and return ‘false’. So make sure your function runs after the page is completely loaded.

    To summarize: swap ‘document.body’ to ‘document.body.outerHTML’, check for CSP errors, and ensure you’re running the function after the page is fully loaded.