From cb3778c686aa37b02615f055fa8aafd1a1dec9b7 Mon Sep 17 00:00:00 2001 From: SamirLiu Date: Fri, 25 Apr 2025 11:28:52 +0800 Subject: [PATCH 1/7] 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 From a7315b6bfc4cf2dc0266740cc479254d7db4dd66 Mon Sep 17 00:00:00 2001 From: SamirLiu Date: Fri, 25 Apr 2025 12:38:36 +0800 Subject: [PATCH 2/7] feat: add google tag manager config --- _config.yml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/_config.yml b/_config.yml index fc2606a..fb00646 100644 --- a/_config.yml +++ b/_config.yml @@ -702,6 +702,12 @@ umami_analytics: # Umami Cloud (API key) / self-hosted Umami (token) token: +# https://www.googletagmanager.com/ +google_tag_manager: + tag_id: + # optional + domain: + # -------------------------------------- # Advertisement # -------------------------------------- From 9c5294b40c1a8a224223a1f14830b11dbe3f8a89 Mon Sep 17 00:00:00 2001 From: SamirLiu Date: Wed, 7 May 2025 19:21:39 +0800 Subject: [PATCH 3/7] fix: update analytics.pug for google tag manager under pjax --- layout/includes/head/analytics.pug | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/layout/includes/head/analytics.pug b/layout/includes/head/analytics.pug index 7412c41..ce54837 100644 --- a/layout/includes/head/analytics.pug +++ b/layout/includes/head/analytics.pug @@ -35,8 +35,14 @@ if 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}'); + window.initGTM = function() { + (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}'); + }; + window.initGTM(); + btf.addGlobalFn('pjaxComplete', () => { + window.initGTM(); + }, 'google_tag_manager'); \ No newline at end of file From 5bac44a284907f8ca5ff6a4b3986e331c8da9370 Mon Sep 17 00:00:00 2001 From: SamirLiu Date: Thu, 8 May 2025 13:51:09 +0800 Subject: [PATCH 4/7] feat: add dataLayer push event pjaxComplete --- layout/includes/head/analytics.pug | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/layout/includes/head/analytics.pug b/layout/includes/head/analytics.pug index ce54837..3d07d61 100644 --- a/layout/includes/head/analytics.pug +++ b/layout/includes/head/analytics.pug @@ -35,14 +35,11 @@ if theme.microsoft_clarity if (theme.google_tag_manager && theme.google_tag_manager.tag_id) script. - window.initGTM = function() { - (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}'); - }; - window.initGTM(); + (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}'); btf.addGlobalFn('pjaxComplete', () => { - window.initGTM(); + dataLayer.push({'event': 'pjaxComplete', 'page_path': window.location.pathname, 'page_title': document.title}); }, 'google_tag_manager'); \ No newline at end of file From b7f1610859525d241c1a09e6bc321907de078f9a Mon Sep 17 00:00:00 2001 From: SamirLiu Date: Thu, 8 May 2025 14:03:08 +0800 Subject: [PATCH 5/7] update: DataLayer parameter --- layout/includes/head/analytics.pug | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/layout/includes/head/analytics.pug b/layout/includes/head/analytics.pug index 3d07d61..6357b7a 100644 --- a/layout/includes/head/analytics.pug +++ b/layout/includes/head/analytics.pug @@ -41,5 +41,5 @@ if (theme.google_tag_manager && theme.google_tag_manager.tag_id) "!{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}'); btf.addGlobalFn('pjaxComplete', () => { - dataLayer.push({'event': 'pjaxComplete', 'page_path': window.location.pathname, 'page_title': document.title}); + dataLayer.push({'event': 'pjaxComplete', 'page_title': document.title, 'page_location': location.href, 'page_path': window.location.pathname}); }, 'google_tag_manager'); \ No newline at end of file From 023d82820d4a518d3b9b7a579044ed9889f81b4a Mon Sep 17 00:00:00 2001 From: SamirLiu Date: Fri, 9 May 2025 13:23:17 +0800 Subject: [PATCH 6/7] feat: update merge_config.js --- scripts/events/merge_config.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/scripts/events/merge_config.js b/scripts/events/merge_config.js index e6bf36d..be30fb2 100644 --- a/scripts/events/merge_config.js +++ b/scripts/events/merge_config.js @@ -388,6 +388,10 @@ hexo.extend.filter.register('before_generate', () => { crisp: { website_id: null }, + google_tag_manager: { + tag_id: null, + domain: 'https://www.googletagmanager.com' + }, baidu_analytics: null, google_analytics: null, cloudflare_analytics: null, From baee803689e59ca254700fac255beca323abd58f Mon Sep 17 00:00:00 2001 From: SamirLiu Date: Fri, 9 May 2025 13:28:01 +0800 Subject: [PATCH 7/7] feat: move noscript to additional-js --- layout/includes/additional-js.pug | 6 +++++- layout/includes/layout.pug | 4 ---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/layout/includes/additional-js.pug b/layout/includes/additional-js.pug index 5312260..6467864 100644 --- a/layout/includes/additional-js.pug +++ b/layout/includes/additional-js.pug @@ -54,4 +54,8 @@ div if theme.busuanzi.site_uv || theme.busuanzi.site_pv || theme.busuanzi.page_pv script(async data-pjax src= theme.asset.busuanzi || '//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js') - != partial('includes/third-party/search/index', {}, { cache: true }) \ No newline at end of file + != partial('includes/third-party/search/index', {}, { cache: true }) + + 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") \ No newline at end of file diff --git a/layout/includes/layout.pug b/layout/includes/layout.pug index e8f6979..ef6a46b 100644 --- a/layout/includes/layout.pug +++ b/layout/includes/layout.pug @@ -10,10 +10,6 @@ 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