diff --git a/integrations/ahrefs/CHANGELOG.md b/integrations/ahrefs/CHANGELOG.md new file mode 100644 index 000000000..0ff4ee50d --- /dev/null +++ b/integrations/ahrefs/CHANGELOG.md @@ -0,0 +1,7 @@ +# @gitbook/integration-ahrefs + +## 0.0.1 + +### Patch Changes + +Added Ahrefs Web Analytics integration. diff --git a/integrations/ahrefs/assets/ahrefs-preview.png b/integrations/ahrefs/assets/ahrefs-preview.png new file mode 100644 index 000000000..5fb4c7847 Binary files /dev/null and b/integrations/ahrefs/assets/ahrefs-preview.png differ diff --git a/integrations/ahrefs/assets/icon.png b/integrations/ahrefs/assets/icon.png new file mode 100644 index 000000000..25e1b6fc7 Binary files /dev/null and b/integrations/ahrefs/assets/icon.png differ diff --git a/integrations/ahrefs/gitbook-manifest.yaml b/integrations/ahrefs/gitbook-manifest.yaml new file mode 100644 index 000000000..7b02ca17a --- /dev/null +++ b/integrations/ahrefs/gitbook-manifest.yaml @@ -0,0 +1,42 @@ +name: ahrefs +title: Ahrefs +icon: ./assets/icon.png +previewImages: + - ./assets/ahrefs-preview.png +description: Receive GitBook traffic insights directly in your Ahrefs dashboard. +externalLinks: + - label: Documentation + url: https://www.gitbook.com/integrations/ahrefs +visibility: public +script: ./src/index.ts +# The following scope(s) are available only to GitBook Staff +# See https://developer.gitbook.com/integrations/configurations#scopes +scopes: + - site:script:inject +organization: gitbook +contentSecurityPolicy: + script-src: https://analytics.ahrefs.com + connect-src: analytics.ahrefs.com +summary: | + # Overview + [Ahrefs Web Analytics](https://ahrefs.com/web-analytics) is a privacy-friendly analytics platform that provides you with a simple and free solution to track and report your published spaces' traffic. No cookies by default. Just the metrics you need. + + # How it works + The Ahrefs integration allows you to track traffic in your published spaces from your Ahrefs dashboard. + + Automatic tracking in your documentation: Each of your connected GitBook spaces will fetch the Ahrefs Web Analytics tracking script and inject it in your public content. + + # Configure + Enable the Ahrefs integration by navigating to organization settings. The integration can be enabled on single or multiple spaces available publicly. +categories: + - analytics +configurations: + site: + properties: + tracking_id: + type: string + title: Tracking ID + description: Look for this in your Ahrefs Web Analytics account. + required: + - tracking_id +target: site diff --git a/integrations/ahrefs/package.json b/integrations/ahrefs/package.json new file mode 100644 index 000000000..53518a5b8 --- /dev/null +++ b/integrations/ahrefs/package.json @@ -0,0 +1,19 @@ +{ + "name": "@gitbook/integration-ahrefs", + "version": "0.0.1", + "private": true, + "dependencies": { + "@gitbook/api": "*", + "@gitbook/runtime": "*" + }, + "devDependencies": { + "@gitbook/cli": "workspace:*", + "@gitbook/tsconfig": "workspace:*" + }, + "scripts": { + "typecheck": "tsc --noEmit", + "publish-integrations-staging": "gitbook publish .", + "check": "gitbook check", + "publish-integrations": "gitbook publish ." + } +} diff --git a/integrations/ahrefs/src/index.ts b/integrations/ahrefs/src/index.ts new file mode 100644 index 000000000..4c101b396 --- /dev/null +++ b/integrations/ahrefs/src/index.ts @@ -0,0 +1,42 @@ +import { + createIntegration, + FetchPublishScriptEventCallback, + RuntimeContext, + RuntimeEnvironment, +} from '@gitbook/runtime'; + +import script from './script.raw.js'; + +type AhrefsRuntimeContext = RuntimeContext< + RuntimeEnvironment< + {}, + { + tracking_id?: string; + } + > +>; + +export const handleFetchEvent: FetchPublishScriptEventCallback = async ( + event, + { environment }: AhrefsRuntimeContext, +) => { + const trackingId = environment.siteInstallation?.configuration?.tracking_id; + if (!trackingId) { + throw new Error( + `The Ahrefs tracking ID is missing from the configuration (ID: ${ + 'spaceId' in event ? event.spaceId : event.siteId + }).`, + ); + } + + return new Response((script as string).replace('', trackingId), { + headers: { + 'Content-Type': 'application/javascript', + 'Cache-Control': 'max-age=604800', + }, + }); +}; + +export default createIntegration({ + fetch_published_script: handleFetchEvent, +}); diff --git a/integrations/ahrefs/src/script.raw.js b/integrations/ahrefs/src/script.raw.js new file mode 100644 index 000000000..2f78da6a7 --- /dev/null +++ b/integrations/ahrefs/src/script.raw.js @@ -0,0 +1,10 @@ +(function (win, doc) { + const trackingID = ''; + const js = doc.createElement('script'); + js.type = 'text/javascript' + js.async = true; + js.src = 'https://analytics.ahrefs.com/analytics.js'; + js.dataset['key'] = trackingID; + const first = doc.getElementsByTagName('script')[0]; + first.parentNode.insertBefore(js, first); +})(window, document); diff --git a/integrations/ahrefs/tsconfig.json b/integrations/ahrefs/tsconfig.json new file mode 100644 index 000000000..1a48f875b --- /dev/null +++ b/integrations/ahrefs/tsconfig.json @@ -0,0 +1,3 @@ +{ + "extends": "@gitbook/tsconfig/integration.json" +}