Spaces:
Build error
Build error
Adrien Denat
commited on
Fix gtm loading (#139)
Browse files* try to get GA env to load
* rollback to injection from the server hook
* remove dead code
- src/app.html +6 -3
- src/hooks.server.ts +14 -1
src/app.html
CHANGED
|
@@ -12,6 +12,9 @@
|
|
| 12 |
) {
|
| 13 |
document.documentElement.classList.add("dark");
|
| 14 |
}
|
|
|
|
|
|
|
|
|
|
| 15 |
</script>
|
| 16 |
%sveltekit.head%
|
| 17 |
</head>
|
|
@@ -20,9 +23,9 @@
|
|
| 20 |
|
| 21 |
<!-- Google Tag Manager -->
|
| 22 |
<script>
|
| 23 |
-
if (
|
| 24 |
const script = document.createElement("script");
|
| 25 |
-
script.src = "https://www.googletagmanager.com/gtag/js?id
|
| 26 |
script.async = true;
|
| 27 |
document.head.appendChild(script);
|
| 28 |
|
|
@@ -32,7 +35,7 @@
|
|
| 32 |
}
|
| 33 |
gtag("js", new Date());
|
| 34 |
/// ^ See https://developers.google.com/tag-platform/gtagjs/install
|
| 35 |
-
gtag("config",
|
| 36 |
gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" });
|
| 37 |
/// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent
|
| 38 |
/// TODO: ask the user for their consent and update this with gtag('consent', 'update')
|
|
|
|
| 12 |
) {
|
| 13 |
document.documentElement.classList.add("dark");
|
| 14 |
}
|
| 15 |
+
|
| 16 |
+
// For some reason, Sveltekit doesn't let us load env variables from .env here, so we load it from hooks.server.ts
|
| 17 |
+
window.gaId = "%gaId%";
|
| 18 |
</script>
|
| 19 |
%sveltekit.head%
|
| 20 |
</head>
|
|
|
|
| 23 |
|
| 24 |
<!-- Google Tag Manager -->
|
| 25 |
<script>
|
| 26 |
+
if (window.gaId) {
|
| 27 |
const script = document.createElement("script");
|
| 28 |
+
script.src = "https://www.googletagmanager.com/gtag/js?id=" + window.gaId;
|
| 29 |
script.async = true;
|
| 30 |
document.head.appendChild(script);
|
| 31 |
|
|
|
|
| 35 |
}
|
| 36 |
gtag("js", new Date());
|
| 37 |
/// ^ See https://developers.google.com/tag-platform/gtagjs/install
|
| 38 |
+
gtag("config", window.gaId);
|
| 39 |
gtag("consent", "default", { ad_storage: "denied", analytics_storage: "denied" });
|
| 40 |
/// ^ See https://developers.google.com/tag-platform/gtagjs/reference#consent
|
| 41 |
/// TODO: ask the user for their consent and update this with gtag('consent', 'update')
|
src/hooks.server.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
import { dev } from "$app/environment";
|
| 2 |
import { COOKIE_NAME } from "$env/static/private";
|
| 3 |
import type { Handle } from "@sveltejs/kit";
|
|
|
|
| 4 |
import { addYears } from "date-fns";
|
| 5 |
|
| 6 |
export const handle: Handle = async ({ event, resolve }) => {
|
|
@@ -18,7 +19,19 @@ export const handle: Handle = async ({ event, resolve }) => {
|
|
| 18 |
expires: addYears(new Date(), 1),
|
| 19 |
});
|
| 20 |
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
return response;
|
| 24 |
};
|
|
|
|
| 1 |
import { dev } from "$app/environment";
|
| 2 |
import { COOKIE_NAME } from "$env/static/private";
|
| 3 |
import type { Handle } from "@sveltejs/kit";
|
| 4 |
+
import { PUBLIC_GOOGLE_ANALYTICS_ID } from "$env/static/public";
|
| 5 |
import { addYears } from "date-fns";
|
| 6 |
|
| 7 |
export const handle: Handle = async ({ event, resolve }) => {
|
|
|
|
| 19 |
expires: addYears(new Date(), 1),
|
| 20 |
});
|
| 21 |
|
| 22 |
+
let replaced = false;
|
| 23 |
+
|
| 24 |
+
const response = await resolve(event, {
|
| 25 |
+
transformPageChunk: (chunk) => {
|
| 26 |
+
// For some reason, Sveltekit doesn't let us load env variables from .env in the app.html template
|
| 27 |
+
if (replaced || !chunk.html.includes("%gaId%")) {
|
| 28 |
+
return chunk.html;
|
| 29 |
+
}
|
| 30 |
+
replaced = true;
|
| 31 |
+
|
| 32 |
+
return chunk.html.replace("%gaId%", PUBLIC_GOOGLE_ANALYTICS_ID);
|
| 33 |
+
},
|
| 34 |
+
});
|
| 35 |
|
| 36 |
return response;
|
| 37 |
};
|