How to Integrate GTM with Partytown in Django Template

95 Views Asked by At

I am trying to integrate my GTM tag using Django Template. Following is my current html snipped that is working perfectly fine without partytown integration.

<head>
<script>
                (function (w, d, s, l, i) {
                    w[l] = w[l] || [];
                    w[l].push({
                        'gtm.start':
                            new Date().getTime(), event: 'gtm.js'
                    });
                    var f = d.getElementsByTagName(s)[0],
                        j = d.createElement(s), dl = l != 'dataLayer' ? '&l=' + l : '';
                    j.async = true;
                    j.src =
                        'https://www.googletagmanager.com/gtm.js?id=' + i + dl;
                    f.parentNode.insertBefore(j, f);
                })(window, document, 'script', 'dataLayer', '<gtm-id>');
            </script>
</head>
<body>
<noscript>
            <iframe src="https://www.googletagmanager.com/ns.html?id=<gtm-id>" height="0" width="0"
                    style="display:none;visibility:hidden"></iframe>
        </noscript>
</body>

Now i want to integrate it throught partytown approach, something I tried was following but doesnot work

<script type="text/partytown" src="https://www.googletagmanager.com/gtag/js?id=YOUR-ID-HERE"></script>
  <script type="text/partytown">
    window.dataLayer = window.dataLayer || [];
    function gtag(){dataLayer.push(arguments);}
    gtag('js', new Date());

    gtag('config', 'YOUR-ID-HERE');
  </script>

I just replaced my GTM ID in above template, this is the piece of code given in their documentation.

If any one can integrate the conventional GTM code into the above partytown snipper please help, Im new to this library.

Also note that I have integrated partytown library in my project so that is not a problem. No errors there just dont know how to integrate it. Thanks in advance

0

There are 0 best solutions below