skip to Main Content

My host does not allow to publish code with javascript and I would like to know if it is possible to use GTM for this. In this case I will create a static page with a div and check the ID in GTM, if true I will run the script.

if (document.getElementById("id")) {

    // the script here
}

Does this idea make sense?

I’m asking before testing, because the page in this case is an e-commerce, and every time I publish a new version in GTM the performance drops drastically.

2

Answers


  1. Chosen as BEST ANSWER

    Yes it's possible, but is considered a bad practice, according to a deleted comment, the scripts need to run after full loaded page.

    Something like this:

    document.addEventListener('DOMContentLoaded', function() {
    
       if (document.getElementById("id")) {    
           // the script here
       }
    
    }, false);
    

  2. Technically, yes (from question and comment I gather that this is some sort of page editor that does not allow to include custom JS, but provides GTM access; if you were flat out not allowed to use JS, GTM would not work, either).

    Good idea? No, for at least three reasons:

    • This is not performant at all, since each bit of custom script will be eval’d (unless you use custom templates, but those cannot add things to the DOM).
    • It is not super easy to maintain (YMMV), since you would need to add tags for your content and then update the tags with your links and navigation (conceivably you could automate this via the GTM API)
    • You would be relying on free infrastructure with no Service Level Agreements for a business critical function, which is not a great idea.

    It would probably be easier to change hosts (and/or use some server-side technology to generate the website for you).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search