I have 2 questions related to GTM implementation
What's the best way to add GTM in the body. I'm adding it using SSI file which is placed immediately after the tag.
<!--#include virtual="/local-assets/gtm-noscript.html" -->
Is it ok to include it like that or is there any better way to add it after the tag?
<body>
<noscript><iframe src="//www.googletagmanager.com/ns.html?id=GTM-YYYY"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<!-- End Google Tag Manager (noscript) -->
Secondly, I am implementing dataLayer.push code using Javascript onclick event. Is this a good practice to hard code dataLayer.push on the page like the example below?
<a href="#" name="button1" onclick="dataLayer.push({'event': 'button1-click'});" >Button</a>
I have to add onclick dataLayer.push events on at least 50 pages and all are static html pages and each page will have unique dataLayer variables.
Any suggestions on the best practices and how to best implement it would be much appreciated!
I can't answer your first question because I myself am not sure if your method is better or the default is preferred.
However I do know that best practices for Javascript listening events is to have the click trigger a function that you place elsewhere instead of inline. If it's not a complicated process then you should certainly separate it.
There are cases where separating it is not practical. For example in my last implementation, I pushed the dataLayer code directly from the addToCart button because the system I was implementing it on pulls data to populate the product list once, then loops through the table rows to generate the listing. The problem was that there were hundreds of products. Doing separate db queries would impact performance severely so instead I just relied on the first results set and queued my dataLayer processing code into the original loop.