@hagicode/hagilight-core 0.9.2 → 0.9.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/GoogleAnalytics.astro +9 -6
- package/package.json +1 -1
package/GoogleAnalytics.astro
CHANGED
|
@@ -6,17 +6,20 @@ interface Props {
|
|
|
6
6
|
const { measurementId } = Astro.props;
|
|
7
7
|
if (!measurementId?.trim()) throw new Error('GoogleAnalytics requires a measurementId prop.');
|
|
8
8
|
const scriptUrl = `https://www.googletagmanager.com/gtag/js?id=${encodeURIComponent(measurementId)}`;
|
|
9
|
+
// Astro wraps a script that receives template variables in an IIFE, which would keep gtag out of
|
|
10
|
+
// the global scope and silently disable the click-event listener below. Assign window.gtag explicitly.
|
|
11
|
+
const initScript = [
|
|
12
|
+
'window.dataLayer = window.dataLayer || [];',
|
|
13
|
+
'window.gtag = function gtag(){ window.dataLayer.push(arguments); };',
|
|
14
|
+
"window.gtag('js', new Date());",
|
|
15
|
+
`window.gtag('config', ${JSON.stringify(measurementId).replace(/</g, '\\u003c')});`,
|
|
16
|
+
].join('\n');
|
|
9
17
|
---
|
|
10
18
|
|
|
11
19
|
{import.meta.env.PROD && (
|
|
12
20
|
<>
|
|
13
21
|
<script async src={scriptUrl}></script>
|
|
14
|
-
<script is:inline
|
|
15
|
-
window.dataLayer = window.dataLayer || [];
|
|
16
|
-
function gtag(){dataLayer.push(arguments);}
|
|
17
|
-
gtag('js', new Date());
|
|
18
|
-
gtag('config', measurementId);
|
|
19
|
-
</script>
|
|
22
|
+
<script is:inline set:html={initScript}></script>
|
|
20
23
|
</>
|
|
21
24
|
)}
|
|
22
25
|
|