From cb3778c686aa37b02615f055fa8aafd1a1dec9b7 Mon Sep 17 00:00:00 2001 From: SamirLiu Date: Fri, 25 Apr 2025 11:28:52 +0800 Subject: [PATCH] feat: add google tag manager noscript --- layout/includes/head/analytics.pug | 10 +++++++++- layout/includes/layout.pug | 4 ++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/layout/includes/head/analytics.pug b/layout/includes/head/analytics.pug index e6aa621..7412c41 100644 --- a/layout/includes/head/analytics.pug +++ b/layout/includes/head/analytics.pug @@ -31,4 +31,12 @@ if theme.microsoft_clarity c[a]=c[a]||function(){(c[a].q=c[a].q||[]).push(arguments)}; t=l.createElement(r);t.async=1;t.src="https://www.clarity.ms/tag/"+i; y=l.getElementsByTagName(r)[0];y.parentNode.insertBefore(t,y); - })(window, document, "clarity", "script", "!{theme.microsoft_clarity}"); \ No newline at end of file + })(window, document, "clarity", "script", "!{theme.microsoft_clarity}"); + +if (theme.google_tag_manager && theme.google_tag_manager.tag_id) + 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= + "!{theme.google_tag_manager.domain ? theme.google_tag_manager.domain : 'https://www.googletagmanager.com'}/gtm.js?id="+i+dl;f.parentNode.insertBefore(j,f); + })(window,document,'script','dataLayer','!{theme.google_tag_manager.tag_id}'); diff --git a/layout/includes/layout.pug b/layout/includes/layout.pug index ef6a46b..e8f6979 100644 --- a/layout/includes/layout.pug +++ b/layout/includes/layout.pug @@ -10,6 +10,10 @@ html(lang=config.language data-theme=theme.display_mode class=htmlClassHideAside head include ./head.pug body + if (theme.google_tag_manager && theme.google_tag_manager.tag_id) + noscript + iframe(src=`${theme.google_tag_manager.domain ? theme.google_tag_manager.domain : 'https://www.googletagmanager.com'}/ns.html?id=${theme.google_tag_manager.tag_id}` height="0" width="0" style="display:none;visibility:hidden") + !=partial('includes/loading/index', {}, {cache: true}) if theme.background