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')