First attempt to add Google Analytics to SvelteKit. This should be per site.
This commit is contained in:
39
src/lib/analytics.svelte
Normal file
39
src/lib/analytics.svelte
Normal file
@@ -0,0 +1,39 @@
|
||||
<script lang="ts">
|
||||
// import { page } from '$app/stores'
|
||||
|
||||
export let site_google_tracking_id: string = '';
|
||||
console.log(`site_google_tracking_id = `, site_google_tracking_id);
|
||||
|
||||
if (typeof window !== 'undefined') {
|
||||
window.dataLayer = window.dataLayer || [];
|
||||
window.gtag = function gtag(): void {
|
||||
window.dataLayer.push(arguments);
|
||||
};
|
||||
window.gtag('js', new Date());
|
||||
window.gtag('config', site_google_tracking_id);
|
||||
}
|
||||
|
||||
// $: if (typeof gtag !== 'undefined') {
|
||||
// gtag('config', site_google_tracking_id, {
|
||||
// page_title: document.title,
|
||||
// page_path: $page.url.pathname,
|
||||
// })
|
||||
// }
|
||||
</script>
|
||||
|
||||
<svelte:head>
|
||||
<script
|
||||
async
|
||||
src="https://www.googletagmanager.com/gtag/js?id={site_google_tracking_id}">
|
||||
</script>
|
||||
<!-- <script>
|
||||
window.dataLayer = window.dataLayer || []
|
||||
|
||||
function gtag() {
|
||||
dataLayer.push(arguments)
|
||||
}
|
||||
|
||||
gtag('js', new Date())
|
||||
gtag('config', site_google_tracking_id)
|
||||
</script> -->
|
||||
</svelte:head>
|
||||
Reference in New Issue
Block a user