Skip to content

Ahrefs Web Analytics integration #774

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/stupid-points-taste.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@gitbook/integration-ahrefs": patch
---

Ahrefs Web Analytics integration
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/)
Expand Down
14 changes: 14 additions & 0 deletions bun.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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"],
Expand Down
7 changes: 7 additions & 0 deletions integrations/ahrefs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# @gitbook/integration-ahrefs

## 0.1.0

### Minor Changes

Added Ahrefs Web Analytics integration.
Binary file added integrations/ahrefs/assets/ahrefs-preview.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added integrations/ahrefs/assets/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
42 changes: 42 additions & 0 deletions integrations/ahrefs/gitbook-manifest.yaml
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions integrations/ahrefs/package.json
Original file line number Diff line number Diff line change
@@ -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 ."
}
}
38 changes: 38 additions & 0 deletions integrations/ahrefs/src/index.ts
Original file line number Diff line number Diff line change
@@ -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('<TO_REPLACE>', trackingId), {
headers: {
'Content-Type': 'application/javascript',
'Cache-Control': 'max-age=604800',
},
});
};

export default createIntegration<AhrefsRuntimeContext>({
fetch_published_script: handleFetchEvent,
});
10 changes: 10 additions & 0 deletions integrations/ahrefs/src/script.raw.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
(function (win, doc) {
const trackingID = '<TO_REPLACE>';
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);
3 changes: 3 additions & 0 deletions integrations/ahrefs/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "@gitbook/tsconfig/integration.json"
}