Tolga Caglayan
Tolga Caglayan

Reputation: 1

The script does not run when the page changes with Astrojs ViewTranstation

I am using Astrojs ViewTranstation. The scripts do not work and are not reloaded when the page is changed. I am having this problem with Google Tag. Does anyone have a solution?

<div id="disqus_thread" class="py-12"></div>
<script  defer>
    function loadDisqus() {
        if (window.DISQUS) {
            window.DISQUS.reset({
                reload: true,
                config: function () {
                    this.page.url = window.location.href;
                    this.page.identifier = window.location.pathname;
                }
            });
        } else {
            var d = document,
                s = d.createElement("script");
            s.src = "https://###/embed.js";
            s.setAttribute("data-timestamp", +new Date());
            (d.head || d.body).appendChild(s);
        }
    }

    document.addEventListener("astro:page-load", loadDisqus);

    document.addEventListener("DOMContentLoaded", loadDisqus);
</script>



<noscript>
    Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a>
</noscript>

I tried the following for Google Tag and it still doesn't work when the page is changed.

<script 
  async 
  src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXXXXXX"
></script>
<script is:inline>
  window.dataLayer = window.dataLayer || [];
  function gtag() {
    dataLayer.push(arguments);
  }
</script>
<script is:inline>
  document.addEventListener(
    "astro:page-load",
    () => {
      gtag("js", new Date());
      gtag("config", "G-XXXXXXXXXX");
    },
    { once: false }
  );
</script>

Upvotes: 0

Views: 30

Answers (0)

Related Questions