diff --git a/.github/workflows/_release_docs.yaml b/.github/workflows/_release_docs.yaml index e9e12be2..e0f5e0c5 100644 --- a/.github/workflows/_release_docs.yaml +++ b/.github/workflows/_release_docs.yaml @@ -68,6 +68,7 @@ jobs: run: uv run poe build-docs env: APIFY_SIGNING_TOKEN: ${{ secrets.APIFY_SIGNING_TOKEN }} + SEGMENT_TOKEN: ${{ secrets.SEGMENT_TOKEN }} - name: Set up GitHub Pages uses: actions/configure-pages@v5 diff --git a/website/docusaurus.config.js b/website/docusaurus.config.js index 4a1a428f..92406f80 100644 --- a/website/docusaurus.config.js +++ b/website/docusaurus.config.js @@ -1,4 +1,4 @@ -const path = require('path'); +const { join, resolve } = require('node:path'); const { config } = require('@apify/docs-theme'); @@ -125,8 +125,8 @@ module.exports = { routeBasePath: 'reference', python: true, pythonOptions: { - pythonModulePath: path.join(__dirname, '../src/apify'), - moduleShortcutsPath: path.join(__dirname, '/module_shortcuts.json'), + pythonModulePath: join(__dirname, '../src/apify'), + moduleShortcutsPath: join(__dirname, '/module_shortcuts.json'), }, reexports: [ // Storages @@ -263,6 +263,13 @@ module.exports = { ], }, ], + [ + resolve(__dirname, 'src/plugins/docusaurus-plugin-segment'), + { + writeKey: process.env.SEGMENT_TOKEN, + allowedInDev: false, + }, + ], [ '@signalwire/docusaurus-plugin-llms-txt', { diff --git a/website/src/plugins/docusaurus-plugin-segment/index.js b/website/src/plugins/docusaurus-plugin-segment/index.js new file mode 100644 index 00000000..5ee41b4d --- /dev/null +++ b/website/src/plugins/docusaurus-plugin-segment/index.js @@ -0,0 +1,59 @@ +const path = require('node:path'); + +module.exports = function (context, options) { + const { writeKey, allowedInDev = false } = options; + + return { + name: 'docusaurus-plugin-segment', + + getClientModules() { + return [path.resolve(__dirname, './segment')]; + }, + + injectHtmlTags() { + if (process.env.NODE_ENV !== 'production' && !allowedInDev) { + return {}; + } + + if (!writeKey) { + console.warn('You need to specify a Segment writeKey in the plugin options'); + return {}; + } + + return { + headTags: [ + { + tagName: 'script', + attributes: { + src: 'https://cdn.cookielaw.org/scripttemplates/otSDKStub.js', + type: 'text/javascript', + charset: 'UTF-8', + 'data-domain-script': '7a8d334b-f744-4c02-9931-92861196dd3c', + }, + }, + { + tagName: 'script', + attributes: { + type: 'text/javascript', + }, + innerHTML: 'function OptanonWrapper() {}', + }, + { + tagName: 'script', + attributes: { + src: 'https://cdn.jsdelivr.net/npm/@segment/analytics-consent-wrapper-onetrust@latest/dist/umd/analytics-onetrust.umd.js', + }, + }, + { + tagName: 'script', + innerHTML: ` + !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 { + // Don't track page views on development + if (process.env.NODE_ENV === 'production' && window.analytics) { + window.analytics.page({ + app: 'docs', + page_type: 'DOCS_PAGE', + path: window.location.pathname, + url: window.location.href, + search: window.location.search, + ...getOneTrustConsentContext(), + }); + } + }, 0); + }, +} : null;