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 1 commit 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
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.0.1

### Patch 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 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
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.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 ."
}
}
42 changes: 42 additions & 0 deletions integrations/ahrefs/src/index.ts
Original file line number Diff line number Diff line change
@@ -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('<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"
}