Skip to content

Commit

Permalink
docs: fix google analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
JobaDiniz committed May 11, 2023
1 parent cbaf7d5 commit e819976
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 3 deletions.
72 changes: 72 additions & 0 deletions docs/_assets/gtag.js
Original file line number Diff line number Diff line change
@@ -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);
5 changes: 2 additions & 3 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,14 @@
loadSidebar: true,
search: 'auto',
themeColor: '#E57A44',
ga: 'G-4W227SRJTC'
gtag: 'G-4W227SRJTC'
}
</script>
<!-- Docsify v4 -->
<script src="//cdn.jsdelivr.net/npm/docsify@4"></script>
<script src="//cdn.jsdelivr.net/npm/docsify-copy-code/dist/docsify-copy-code.min.js"></script>
<script src="//cdn.jsdelivr.net/npm/[email protected]/components/prism-json.min.js"></script>
<script src="//cdn.jsdelivr.net/npm/docsify/lib/plugins/ga.min.js"></script>
<script src="_assets/gtag.js"></script>
<script src="_assets/prism-rpa.js"></script>
<!--<script src="//cdn.jsdelivr.net/npm/docsify-darklight-theme@latest/dist/index.min.js" type="text/javascript"></script>-->
</body>
</html>

0 comments on commit e819976

Please sign in to comment.