#Order of execution in <script />

2 messages · Page 1 of 1 (latest)

mortal coyote
#

I am initializing some classes under globalThis in my <Layout />
I am trying to access these in a page that uses that Layout.
The function is not there. :

// Layout

<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-XXXXXX");
</script>

<script src="@helpers/tracking.ts"></script>
// tracking.ts
import { DataWarehouse, GoogleAnalytics } from "@visable-dev/tracking";

globalThis.googleAnalytics = new GoogleAnalytics({
  trackingEnv: import.meta.env.TRACKING_ENV,
});
globalThis.dataWarehouse = new DataWarehouse();
// page

<script>
  console.log(globalThis);
  console.log(globalThis.googleAnalytics);

  globalThis.googleAnalytics.trackPageView({
    virtualPageTitle: "connect_landingpage",
    trafficSource: "internal",
  });
</script>
Window {window: Window, self: Window, document: document, name: '', location: Location, …}
wlw-connect.astro:32 undefined
wlw-connect.astro:34 Uncaught TypeError: Cannot read properties of undefined (reading 'trackPageView')
tranquil hearthBOT
#
No-one around right now?

It looks like no-one has responded to your question yet. People might not be available right now or don’t know how to answer your question. Want an answer while you wait? Try asking our experimental bot in #1095492539085230272.