Skip to content

Commit 1d5e1b9

Browse files
authored
feat: add analytics module for tracking page views (#635)
* feat: add analytics module for tracking page views * feat: add analytics module for tracking page views
1 parent dd8fc53 commit 1d5e1b9

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

website/docusaurus.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ const config = {
3535
customFields: {
3636
repoUrl,
3737
},
38+
clientModules: [require.resolve('./src/analytics.ts')],
3839
i18n: {
3940
defaultLocale: 'en',
4041
locales: ['en', 'zh-cn'],

website/src/analytics.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
import ExecutionEnvironment from '@docusaurus/ExecutionEnvironment';
19+
20+
(function () {
21+
if (ExecutionEnvironment.canUseDOM) {
22+
let prefix = '';
23+
if (location.pathname.startsWith('/zh-cn')) {
24+
prefix = '/zh-cn';
25+
}
26+
let s = document.getElementsByTagName('script')[0];
27+
28+
let matomo = document.createElement('script');
29+
matomo.text = `
30+
/* -- Matomo */
31+
var _paq = window._paq = window._paq || [];
32+
/* tracker methods like "setCustomDimension" should be called before "trackPageView" */
33+
/* _paq.push(["setDoNotTrack", true]); */
34+
_paq.push(["disableCookies"]);
35+
_paq.push(['trackPageView']);
36+
_paq.push(['enableLinkTracking']);
37+
(function() {
38+
var u="https://analytics.apache.org/";
39+
_paq.push(['setTrackerUrl', u+'matomo.php']);
40+
_paq.push(['setSiteId', '83']);
41+
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
42+
g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s);
43+
})();
44+
/* End Matomo Code */
45+
`;
46+
s.parentNode.insertBefore(matomo, s);
47+
48+
const controller = new AbortController();
49+
const signal = controller.signal;
50+
// set timeout
51+
setTimeout(() => {
52+
controller.abort();
53+
}, 5000);
54+
fetch(prefix + '/config.json', {signal})
55+
.then((res) => res.json())
56+
.then((data) => {
57+
if (data.analytics) {
58+
}
59+
}).catch((err => {
60+
// do nothing
61+
}));
62+
}
63+
})();

website/static/config.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"analytics": true
3+
}

0 commit comments

Comments
 (0)