Skip to content

Commit fd45ee7

Browse files
authored
docs: Add cookie consent (#1587)
Added cookie consent similar to apify/crawlee#2964, but the one used on web as it's on apify.com domain. - Added Segment OneTrust integration to support cookie modals. - Added consent context to every page view event sent to Segment.
1 parent cf9a9ef commit fd45ee7

File tree

3 files changed

+114
-16
lines changed

3 files changed

+114
-16
lines changed

docusaurus.config.js

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

33
const clsx = require('clsx');
44
const { createApiPageMD } = require('docusaurus-plugin-openapi-docs/lib/markdown');
@@ -46,21 +46,6 @@ module.exports = {
4646
sizes: 'any',
4747
},
4848
},
49-
// Segment analytics
50-
process.env.SEGMENT_TOKEN && {
51-
tagName: 'script',
52-
innerHTML: `
53-
!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="FAretQHrDlKZJl9dE9xRgH2ia0a4ACXB";;analytics.SNIPPET_VERSION="5.2.0";
54-
analytics.load("${process.env.SEGMENT_TOKEN}", { integrations: { "Segment.io": { apiHost: "analytics.apify.com/v1" }}});
55-
analytics.page({
56-
app: 'docs',
57-
pageType: 'DOCS_PAGE',
58-
page_type: 'DOCS_PAGE'
59-
});
60-
}}();
61-
`,
62-
attributes: {},
63-
},
6449
// Intercom messenger
6550
process.env.INTERCOM_APP_ID && {
6651
tagName: 'script',
@@ -250,6 +235,13 @@ module.exports = {
250235
},
251236
},
252237
],
238+
[
239+
resolve(__dirname, 'src/plugins/docusaurus-plugin-segment'),
240+
{
241+
writeKey: process.env.SEGMENT_TOKEN,
242+
allowedInDev: false,
243+
},
244+
],
253245
() => ({
254246
name: 'webpack-loader-fix',
255247
configureWebpack() {
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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 (!writeKey) {
15+
throw new Error(
16+
'You need to specify a Segment writeKey in the plugin options',
17+
);
18+
}
19+
20+
if (process.env.NODE_ENV !== 'production' && !allowedInDev) {
21+
return {};
22+
}
23+
24+
return {
25+
headTags: [
26+
{
27+
tagName: 'script',
28+
attributes: {
29+
src: 'https://cdn.cookielaw.org/scripttemplates/otSDKStub.js',
30+
type: 'text/javascript',
31+
charset: 'UTF-8',
32+
'data-domain-script': '7a8d334b-f744-4c02-9931-92861196dd3c',
33+
},
34+
},
35+
{
36+
tagName: 'script',
37+
attributes: {
38+
type: 'text/javascript',
39+
},
40+
innerHTML: 'function OptanonWrapper() {}',
41+
},
42+
{
43+
tagName: 'script',
44+
attributes: {
45+
src: 'https://cdn.jsdelivr.net/npm/@segment/analytics-consent-wrapper-onetrust@latest/dist/umd/analytics-onetrust.umd.js',
46+
},
47+
},
48+
{
49+
tagName: 'script',
50+
innerHTML: `
51+
!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";
52+
withOneTrust(analytics).load("${writeKey}", { integrations: { "Segment.io": { apiHost: "analytics.apify.com/v1" } } });
53+
}}();
54+
`,
55+
},
56+
],
57+
};
58+
},
59+
};
60+
};
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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({ location }) {
33+
// Don't track page views on development
34+
if (process.env.NODE_ENV === 'production' && window.analytics) {
35+
window.analytics.page({
36+
app: 'docs',
37+
pageType: 'DOCS_PAGE',
38+
page_type: 'DOCS_PAGE',
39+
path: location.pathname,
40+
url: location.href,
41+
search: location.search,
42+
...getOneTrustConsentContext(),
43+
});
44+
}
45+
},
46+
} : null;

0 commit comments

Comments
 (0)