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 # -------------------------------------- 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/head/analytics.pug b/layout/includes/head/analytics.pug index e6aa621..6357b7a 100644 --- a/layout/includes/head/analytics.pug +++ b/layout/includes/head/analytics.pug @@ -31,4 +31,15 @@ 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}'); + btf.addGlobalFn('pjaxComplete', () => { + 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 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,