diff --git a/.changeset/stupid-points-taste.md b/.changeset/stupid-points-taste.md new file mode 100644 index 000000000..0e831746c --- /dev/null +++ b/.changeset/stupid-points-taste.md @@ -0,0 +1,5 @@ +--- +"@gitbook/integration-ahrefs": patch +--- + +Ahrefs Web Analytics integration diff --git a/README.md b/README.md index 4f2ac33ab..eef3edfbb 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,7 @@ Packages are the core of our integration platform, containing the code for the f It also hosts the default integrations provided by the GitBook team: +- [`ahrefs`](./integrations/ahrefs/) - [`arcade`](./integrations/arcade/) - [`fathom`](./integrations/fathom/) - [`figma`](./integrations/figma/) diff --git a/bun.lock b/bun.lock index 96b6d8ba1..c9715dbf3 100644 --- a/bun.lock +++ b/bun.lock @@ -14,6 +14,18 @@ "wrangler": "^2.1.12", }, }, + "integrations/ahrefs": { + "name": "@gitbook/integration-ahrefs", + "version": "0.1.0", + "dependencies": { + "@gitbook/api": "*", + "@gitbook/runtime": "*", + }, + "devDependencies": { + "@gitbook/cli": "workspace:*", + "@gitbook/tsconfig": "workspace:*", + }, + }, "integrations/arcade": { "name": "@gitbook/integration-arcade", "version": "0.2.2", @@ -941,6 +953,8 @@ "@gitbook/document": ["@gitbook/document@workspace:packages/document"], + "@gitbook/integration-ahrefs": ["@gitbook/integration-ahrefs@workspace:integrations/ahrefs"], + "@gitbook/integration-arcade": ["@gitbook/integration-arcade@workspace:integrations/arcade"], "@gitbook/integration-bucket": ["@gitbook/integration-bucket@workspace:integrations/bucket"], diff --git a/integrations/ahrefs/CHANGELOG.md b/integrations/ahrefs/CHANGELOG.md new file mode 100644 index 000000000..57adef1f8 --- /dev/null +++ b/integrations/ahrefs/CHANGELOG.md @@ -0,0 +1,7 @@ +# @gitbook/integration-ahrefs + +## 0.1.0 + +### Minor 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..53f5f38a2 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..e5bb6379b --- /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 sites' traffic. No cookies by default. Just the metrics you need. + + # How it works + The Ahrefs integration allows you to track traffic in your published sites from your Ahrefs dashboard. + + Automatic tracking in your documentation: Each of your connected GitBook sites will fetch the Ahrefs Web Analytics tracking script and inject it in your published content. + + # Configure + Enable the Ahrefs integration by navigating to organization settings. The integration can be enabled on single or multiple sites. +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..6b4e61c46 --- /dev/null +++ b/integrations/ahrefs/package.json @@ -0,0 +1,19 @@ +{ + "name": "@gitbook/integration-ahrefs", + "version": "0.1.0", + "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..bff58978d --- /dev/null +++ b/integrations/ahrefs/src/index.ts @@ -0,0 +1,38 @@ +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) { + return; + } + + 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" +}