Skip to content

Commit aca0fcd

Browse files
Ahrefs Web Analytics integration (#774)
* Ahrefs Web Analytics integration * Apply suggestions from code review Co-authored-by: Taran Vohra <[email protected]> * Apply suggestions from code review * Use a higher-res logo * Bump version and update bun.lock * Update README.md * Create changeset * Add missing semicolon --------- Co-authored-by: Taran Vohra <[email protected]>
1 parent 3b568e5 commit aca0fcd

File tree

11 files changed

+139
-0
lines changed

11 files changed

+139
-0
lines changed

.changeset/stupid-points-taste.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@gitbook/integration-ahrefs": patch
3+
---
4+
5+
Ahrefs Web Analytics integration

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Packages are the core of our integration platform, containing the code for the f
2424

2525
It also hosts the default integrations provided by the GitBook team:
2626

27+
- [`ahrefs`](./integrations/ahrefs/)
2728
- [`arcade`](./integrations/arcade/)
2829
- [`fathom`](./integrations/fathom/)
2930
- [`figma`](./integrations/figma/)

bun.lock

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@
1414
"wrangler": "^2.1.12",
1515
},
1616
},
17+
"integrations/ahrefs": {
18+
"name": "@gitbook/integration-ahrefs",
19+
"version": "0.1.0",
20+
"dependencies": {
21+
"@gitbook/api": "*",
22+
"@gitbook/runtime": "*",
23+
},
24+
"devDependencies": {
25+
"@gitbook/cli": "workspace:*",
26+
"@gitbook/tsconfig": "workspace:*",
27+
},
28+
},
1729
"integrations/arcade": {
1830
"name": "@gitbook/integration-arcade",
1931
"version": "0.2.2",
@@ -967,6 +979,8 @@
967979

968980
"@gitbook/document": ["@gitbook/document@workspace:packages/document"],
969981

982+
"@gitbook/integration-ahrefs": ["@gitbook/integration-ahrefs@workspace:integrations/ahrefs"],
983+
970984
"@gitbook/integration-arcade": ["@gitbook/integration-arcade@workspace:integrations/arcade"],
971985

972986
"@gitbook/integration-bucket": ["@gitbook/integration-bucket@workspace:integrations/bucket"],

integrations/ahrefs/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# @gitbook/integration-ahrefs
2+
3+
## 0.1.0
4+
5+
### Minor Changes
6+
7+
Added Ahrefs Web Analytics integration.
441 KB
Loading

integrations/ahrefs/assets/icon.png

4.86 KB
Loading
Lines changed: 42 additions & 0 deletions
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 sites' 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 sites from your Ahrefs dashboard.
26+
27+
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.
28+
29+
# Configure
30+
Enable the Ahrefs integration by navigating to organization settings. The integration can be enabled on single or multiple sites.
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

integrations/ahrefs/package.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "@gitbook/integration-ahrefs",
3+
"version": "0.1.0",
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+
}

integrations/ahrefs/src/index.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
return;
26+
}
27+
28+
return new Response((script as string).replace('<TO_REPLACE>', trackingId), {
29+
headers: {
30+
'Content-Type': 'application/javascript',
31+
'Cache-Control': 'max-age=604800',
32+
},
33+
});
34+
};
35+
36+
export default createIntegration<AhrefsRuntimeContext>({
37+
fetch_published_script: handleFetchEvent,
38+
});

integrations/ahrefs/src/script.raw.js

Lines changed: 10 additions & 0 deletions
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);

0 commit comments

Comments
 (0)