Skip to content

Commit c579c33

Browse files
authored
docs: add analytics support [internal] (#767)
1 parent 3d7d19c commit c579c33

File tree

4 files changed

+118
-3
lines changed

4 files changed

+118
-3
lines changed

.github/workflows/_release_docs.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ jobs:
6868
run: uv run poe build-docs
6969
env:
7070
APIFY_SIGNING_TOKEN: ${{ secrets.APIFY_SIGNING_TOKEN }}
71+
SEGMENT_TOKEN: ${{ secrets.SEGMENT_TOKEN }}
7172

7273
- name: Set up GitHub Pages
7374
uses: actions/configure-pages@v5

website/docusaurus.config.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const path = require('path');
1+
const { join, resolve } = require('node:path');
22

33
const { config } = require('@apify/docs-theme');
44

@@ -125,8 +125,8 @@ module.exports = {
125125
routeBasePath: 'reference',
126126
python: true,
127127
pythonOptions: {
128-
pythonModulePath: path.join(__dirname, '../src/apify'),
129-
moduleShortcutsPath: path.join(__dirname, '/module_shortcuts.json'),
128+
pythonModulePath: join(__dirname, '../src/apify'),
129+
moduleShortcutsPath: join(__dirname, '/module_shortcuts.json'),
130130
},
131131
reexports: [
132132
// Storages
@@ -263,6 +263,13 @@ module.exports = {
263263
],
264264
},
265265
],
266+
[
267+
resolve(__dirname, 'src/plugins/docusaurus-plugin-segment'),
268+
{
269+
writeKey: process.env.SEGMENT_TOKEN,
270+
allowedInDev: false,
271+
},
272+
],
266273
[
267274
'@signalwire/docusaurus-plugin-llms-txt',
268275
{
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
const path = require('node:path');
2+
3+
module.exports = function (context, options) {
4+
const { writeKey, allowedInDev = false } = options;
5+
6+
return {
7+
name: 'docusaurus-plugin-segment',
8+
9+
getClientModules() {
10+
return [path.resolve(__dirname, './segment')];
11+
},
12+
13+
injectHtmlTags() {
14+
if (process.env.NODE_ENV !== 'production' && !allowedInDev) {
15+
return {};
16+
}
17+
18+
if (!writeKey) {
19+
console.warn('You need to specify a Segment writeKey in the plugin options');
20+
return {};
21+
}
22+
23+
return {
24+
headTags: [
25+
{
26+
tagName: 'script',
27+
attributes: {
28+
src: 'https://cdn.cookielaw.org/scripttemplates/otSDKStub.js',
29+
type: 'text/javascript',
30+
charset: 'UTF-8',
31+
'data-domain-script': '7a8d334b-f744-4c02-9931-92861196dd3c',
32+
},
33+
},
34+
{
35+
tagName: 'script',
36+
attributes: {
37+
type: 'text/javascript',
38+
},
39+
innerHTML: 'function OptanonWrapper() {}',
40+
},
41+
{
42+
tagName: 'script',
43+
attributes: {
44+
src: 'https://cdn.jsdelivr.net/npm/@segment/analytics-consent-wrapper-onetrust@latest/dist/umd/analytics-onetrust.umd.js',
45+
},
46+
},
47+
{
48+
tagName: 'script',
49+
innerHTML: `
50+
!function(){var i="analytics",analytics=window[i]=window[i]||[];if(!analytics.initialize)if(analytics.invoked)window.console&&console.error&&console.error("Segment snippet included twice.");else{analytics.invoked=!0;analytics.methods=["trackSubmit","trackClick","trackLink","trackForm","pageview","identify","reset","group","track","ready","alias","debug","page","screen","once","off","on","addSourceMiddleware","addIntegrationMiddleware","setAnonymousId","addDestinationMiddleware","register"];analytics.factory=function(e){return function(){if(window[i].initialized)return window[i][e].apply(window[i],arguments);var n=Array.prototype.slice.call(arguments);if(["track","screen","alias","group","page","identify"].indexOf(e)>-1){var c=document.querySelector("link[rel='canonical']");n.push({__t:"bpc",c:c&&c.getAttribute("href")||void 0,p:location.pathname,u:location.href,s:location.search,t:document.title,r:document.referrer})}n.unshift(e);analytics.push(n);return analytics}};for(var n=0;n<analytics.methods.length;n++){var key=analytics.methods[n];analytics[key]=analytics.factory(key)}analytics.load=function(key,n){var t=document.createElement("script");t.type="text/javascript";t.async=!0;t.setAttribute("data-global-segment-analytics-key",i);t.src="https://cdn.segment.com/analytics.js/v1/" + key + "/analytics.min.js";var r=document.getElementsByTagName("script")[0];r.parentNode.insertBefore(t,r);analytics._loadOptions=n};analytics._writeKey="${writeKey}";;analytics.SNIPPET_VERSION="5.2.0";
51+
withOneTrust(analytics.load("${writeKey}", { integrations: { "Segment.io": { apiHost: "analytics.apify.com/v1" } } }));
52+
}}();
53+
`,
54+
},
55+
],
56+
};
57+
},
58+
};
59+
};
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import ExecutionEnvironment from '@docusaurus/ExecutionEnvironment';
2+
3+
const DEFAULT_CONSENT = {
4+
C0001: false,
5+
C0002: false,
6+
C0003: false,
7+
C0004: false,
8+
C0005: false,
9+
};
10+
11+
function getOneTrustConsentContext() {
12+
const consent = { ...DEFAULT_CONSENT };
13+
// obtain `OptanonConsent` cookie and extract `groups` substring
14+
const match = document.cookie.match(/(^|;\s*)OptanonConsent=[^;]*&groups=([^;&]*)/);
15+
// decode the value and parse it - expected format: [C0001:1, COOO2:0,...]
16+
const input = decodeURIComponent(match?.[2] ?? '').split(',');
17+
18+
for (const chunk of input) {
19+
const [name, value] = chunk.split(':');
20+
21+
// we only want to update specific groups
22+
if (name in consent) {
23+
// just to be extra sure, only "1" is considered to pass
24+
consent[name] = value === '1';
25+
}
26+
}
27+
28+
return consent;
29+
}
30+
31+
export default ExecutionEnvironment.canUseDOM ? {
32+
onRouteUpdate() {
33+
// this forces deferred execution that ensures `window.location` is in sync
34+
setTimeout(() => {
35+
// Don't track page views on development
36+
if (process.env.NODE_ENV === 'production' && window.analytics) {
37+
window.analytics.page({
38+
app: 'docs',
39+
page_type: 'DOCS_PAGE',
40+
path: window.location.pathname,
41+
url: window.location.href,
42+
search: window.location.search,
43+
...getOneTrustConsentContext(),
44+
});
45+
}
46+
}, 0);
47+
},
48+
} : null;

0 commit comments

Comments
 (0)