Skip to content

Commit babd094

Browse files
Ahrefs Web Analytics integration
1 parent 5499cc5 commit babd094

File tree

8 files changed

+123
-0
lines changed

8 files changed

+123
-0
lines changed

Diff for: integrations/ahrefs/CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# @gitbook/integration-ahrefs
2+
3+
## 0.0.1
4+
5+
### Patch Changes
6+
7+
Added Ahrefs Web Analytics integration.

Diff for: integrations/ahrefs/assets/ahrefs-preview.png

441 KB
Loading

Diff for: integrations/ahrefs/assets/icon.png

11 KB
Loading

Diff for: integrations/ahrefs/gitbook-manifest.yaml

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
name: ahrefs
2+
title: Ahrefs
3+
icon: ./assets/icon.png
4+
previewImages:
5+
- ./assets/ahrefs-preview.png
6+
description: Receive GitBook traffic insights directly in your Ahrefs dashboard.
7+
externalLinks:
8+
- label: Documentation
9+
url: https://www.gitbook.com/integrations/ahrefs
10+
visibility: public
11+
script: ./src/index.ts
12+
# The following scope(s) are available only to GitBook Staff
13+
# See https://developer.gitbook.com/integrations/configurations#scopes
14+
scopes:
15+
- site:script:inject
16+
organization: gitbook
17+
contentSecurityPolicy:
18+
script-src: https://analytics.ahrefs.com
19+
connect-src: analytics.ahrefs.com
20+
summary: |
21+
# Overview
22+
[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.
23+
24+
# How it works
25+
The Ahrefs integration allows you to track traffic in your published spaces from your Ahrefs dashboard.
26+
27+
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.
28+
29+
# Configure
30+
Enable the Ahrefs integration by navigating to organization settings. The integration can be enabled on single or multiple spaces available publicly.
31+
categories:
32+
- analytics
33+
configurations:
34+
site:
35+
properties:
36+
tracking_id:
37+
type: string
38+
title: Tracking ID
39+
description: Look for this in your Ahrefs Web Analytics account.
40+
required:
41+
- tracking_id
42+
target: site

Diff for: integrations/ahrefs/package.json

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "@gitbook/integration-ahrefs",
3+
"version": "0.0.1",
4+
"private": true,
5+
"dependencies": {
6+
"@gitbook/api": "*",
7+
"@gitbook/runtime": "*"
8+
},
9+
"devDependencies": {
10+
"@gitbook/cli": "workspace:*",
11+
"@gitbook/tsconfig": "workspace:*"
12+
},
13+
"scripts": {
14+
"typecheck": "tsc --noEmit",
15+
"publish-integrations-staging": "gitbook publish .",
16+
"check": "gitbook check",
17+
"publish-integrations": "gitbook publish ."
18+
}
19+
}

Diff for: integrations/ahrefs/src/index.ts

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import {
2+
createIntegration,
3+
FetchPublishScriptEventCallback,
4+
RuntimeContext,
5+
RuntimeEnvironment,
6+
} from '@gitbook/runtime';
7+
8+
import script from './script.raw.js';
9+
10+
type AhrefsRuntimeContext = RuntimeContext<
11+
RuntimeEnvironment<
12+
{},
13+
{
14+
tracking_id?: string;
15+
}
16+
>
17+
>;
18+
19+
export const handleFetchEvent: FetchPublishScriptEventCallback = async (
20+
event,
21+
{ environment }: AhrefsRuntimeContext,
22+
) => {
23+
const trackingId = environment.siteInstallation?.configuration?.tracking_id;
24+
if (!trackingId) {
25+
throw new Error(
26+
`The Ahrefs tracking ID is missing from the configuration (ID: ${
27+
'spaceId' in event ? event.spaceId : event.siteId
28+
}).`,
29+
);
30+
}
31+
32+
return new Response((script as string).replace('<TO_REPLACE>', trackingId), {
33+
headers: {
34+
'Content-Type': 'application/javascript',
35+
'Cache-Control': 'max-age=604800',
36+
},
37+
});
38+
};
39+
40+
export default createIntegration<AhrefsRuntimeContext>({
41+
fetch_published_script: handleFetchEvent,
42+
});

Diff for: integrations/ahrefs/src/script.raw.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
(function (win, doc) {
2+
const trackingID = '<TO_REPLACE>';
3+
const js = doc.createElement('script');
4+
js.type = 'text/javascript'
5+
js.async = true;
6+
js.src = 'https://analytics.ahrefs.com/analytics.js';
7+
js.dataset['key'] = trackingID;
8+
const first = doc.getElementsByTagName('script')[0];
9+
first.parentNode.insertBefore(js, first);
10+
})(window, document);

Diff for: integrations/ahrefs/tsconfig.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "@gitbook/tsconfig/integration.json"
3+
}

0 commit comments

Comments
 (0)