Skip to content

Commit

Permalink
optimize: configurable analytics (#855)
Browse files Browse the repository at this point in the history
  • Loading branch information
liuqiufeng authored Mar 22, 2024
1 parent b3e01ca commit 5e8dcfc
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 20 deletions.
54 changes: 34 additions & 20 deletions src/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import ExecutionEnvironment from '@docusaurus/ExecutionEnvironment';
(function () {
if (ExecutionEnvironment.canUseDOM) {
let prefix = '';
if (location.pathname.startsWith('/zh-cn')){
if (location.pathname.startsWith('/zh-cn')) {
prefix = '/zh-cn';
}
let s= document.getElementsByTagName('script')[0];
let s = document.getElementsByTagName('script')[0];

let matomo = document.createElement('script');
matomo.text = `
Expand All @@ -28,26 +28,40 @@ import ExecutionEnvironment from '@docusaurus/ExecutionEnvironment';
`;
s.parentNode.insertBefore(matomo, s);

let gtag = document.createElement('script');
gtag.src = prefix + '/js/gtag.js';
gtag.async = true;
s.parentNode.insertBefore(gtag, s);
const controller = new AbortController();
const signal = controller.signal;
// set timeout
setTimeout(() => {
controller.abort();
}, 5000);
fetch(prefix + '/config.json', {signal})
.then((res) => res.json())
.then((data) => {
if (data.analytics) {
let gtag = document.createElement('script');
gtag.src = prefix + '/js/gtag.js';
gtag.async = true;
s.parentNode.insertBefore(gtag, s);

let hm= document.createElement('script');
hm.src = prefix + '/js/hm.js';
hm.async = true;
s.parentNode.insertBefore(hm, s);
let hm = document.createElement('script');
hm.src = prefix + '/js/hm.js';
hm.async = true;
s.parentNode.insertBefore(hm, s);

let aplus_v2= document.createElement('script');
aplus_v2.src = prefix + '/js/aplus_v2.js';
aplus_v2.id = 'beacon-aplus';
aplus_v2.setAttribute('exparams','clog=o&aplus&sidx=aplusSidx&ckx=aplusCkx');
aplus_v2.defer= true;
s.parentNode.insertBefore(aplus_v2, s);
let aplus_v2 = document.createElement('script');
aplus_v2.src = prefix + '/js/aplus_v2.js';
aplus_v2.id = 'beacon-aplus';
aplus_v2.setAttribute('exparams', 'clog=o&aplus&sidx=aplusSidx&ckx=aplusCkx');
aplus_v2.defer = true;
s.parentNode.insertBefore(aplus_v2, s);

let tracker= document.createElement('script');
tracker.src = prefix + '/js/tracker.js';
tracker.defer = true;
s.parentNode.insertBefore(tracker, s);
let tracker = document.createElement('script');
tracker.src = prefix + '/js/tracker.js';
tracker.defer = true;
s.parentNode.insertBefore(tracker, s);
}
}).catch((err => {
// do nothing
}));
}
})();
3 changes: 3 additions & 0 deletions static/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"analytics": true
}

0 comments on commit 5e8dcfc

Please sign in to comment.