From e8199760ce4db66a0c6350bb832b910b59626998 Mon Sep 17 00:00:00 2001 From: JobaDiniz Date: Thu, 11 May 2023 11:53:21 -0300 Subject: [PATCH] docs: fix google analytics --- docs/_assets/gtag.js | 72 ++++++++++++++++++++++++++++++++++++++++++++ docs/index.html | 5 ++- 2 files changed, 74 insertions(+), 3 deletions(-) create mode 100644 docs/_assets/gtag.js diff --git a/docs/_assets/gtag.js b/docs/_assets/gtag.js new file mode 100644 index 0000000..2440d3d --- /dev/null +++ b/docs/_assets/gtag.js @@ -0,0 +1,72 @@ +/* eslint-disable no-console */ +// From ./ga.js + +function appendScript(id) { + const script = document.createElement('script'); + script.async = true; + script.src = 'https://www.googletagmanager.com/gtag/js?id=' + id; + document.body.appendChild(script); + } + + // global site tag instance initialized + function initGlobalSiteTag(id) { + appendScript(id); + + window.dataLayer = window.dataLayer || []; + window.gtag = + window.gtag || + function () { + window.dataLayer.push(arguments); + }; + + window.gtag('js', new Date()); + window.gtag('config', id); + } + + // add additional products to your tag + // https://developers.google.com/tag-platform/gtagjs/install + function initAdditionalTag(id) { + window.gtag('config', id); + } + + function init(ids) { + if (Array.isArray(ids)) { + // set the first id to be a global site tag + initGlobalSiteTag(ids[0]); + + // the rest ids + ids.forEach((id, index) => { + if (index > 0) { + initAdditionalTag(id); + } + }); + } else { + initGlobalSiteTag(ids); + } + } + + function collect() { + if (!window.gtag) { + init($docsify.gtag); + } + + // usage: https://developers.google.com/analytics/devguides/collection/gtagjs/pages + window.gtag('event', 'page_view', { + /* eslint-disable camelcase */ + page_title: document.title, + page_location: location.href, + page_path: location.pathname, + /* eslint-disable camelcase */ + }); + } + + const install = function (hook) { + if (!$docsify.gtag) { + console.error('[Docsify] gtag is required.'); + return; + } + + hook.beforeEach(collect); + }; + + $docsify.plugins = [].concat(install, $docsify.plugins); \ No newline at end of file diff --git a/docs/index.html b/docs/index.html index f44b605..874b0ce 100644 --- a/docs/index.html +++ b/docs/index.html @@ -20,15 +20,14 @@ loadSidebar: true, search: 'auto', themeColor: '#E57A44', - ga: 'G-4W227SRJTC' + gtag: 'G-4W227SRJTC' } - + -