Skip to content
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

Add hubspot integration #417

Open
wants to merge 4 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/eight-candles-tie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@gitbook/integration-hubspot': major
---

Add HubSpot integration
3 changes: 3 additions & 0 deletions integrations/hubspot/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": ["@gitbook/eslint-config/integration"]
}
9 changes: 9 additions & 0 deletions integrations/hubspot/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# @gitbook/integration-hubspot

## 1.0.0

### Major Changes

### Minor Changes

### Patch Changes
Binary file added integrations/hubspot/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.
34 changes: 34 additions & 0 deletions integrations/hubspot/gitbook-manifest.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: hubspot
title: HubSpot
icon: ./assets/icon.png
organization: d8f63b60-89ae-11e7-8574-5927d48c4877
description: Plug your GitBook site to your HubSpot installation.
visibility: public
script: ./src/index.ts
scopes:
- space:script:inject
contentSecurityPolicy:
script-src: hsadspixel.net hs-banner.com hs-analytics.net hs-scripts.com;
summary: |
# Overview
This integration allows to add the HubSpot tracking code on your published GitBook site.

# How it works
The integration injects the HubSpot script on your page, using the configured environment ID,
so that you can get analytics information from your GitBook site directly inside of Heap.

# Configure
Install the integration on the GitBook space of your choice.
Locate the HubSpot Script Loader URL you want to use, which is available in HubSpot's Reports & Analytics Tracking section.
The URL Should be in the form "//{subdomain}.hs-scripts.com/{portalId}.js".
categories:
- analytics
configurations:
space:
properties:
script_loader_url:
type: string
title: HubSpot Script Loader URL
description: The URL to load the script from. Copied form HubSpot's script tracking code script tag src attribute
required:
- script_loader_url
18 changes: 18 additions & 0 deletions integrations/hubspot/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "@gitbook/integration-hubspot",
"version": "1.0.0",
"private": true,
"dependencies": {
"@gitbook/api": "*",
"@gitbook/runtime": "*"
},
"devDependencies": {
"@gitbook/cli": "*"
},
"scripts": {
"lint": "eslint ./src/**/*.ts",
"typecheck": "tsc --noEmit",
"publish-integrations-staging": "gitbook publish .",
"publish-integrations": "gitbook publish ."
}
}
40 changes: 40 additions & 0 deletions integrations/hubspot/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import {
createIntegration,
FetchPublishScriptEventCallback,
RuntimeContext,
RuntimeEnvironment,
} from '@gitbook/runtime';

import script from './script.raw.js';

type HubSpotRuntimeContext = RuntimeContext<
RuntimeEnvironment<
{},
{
script_loader_url?: string;
}
>
>;

export const handleFetchEvent: FetchPublishScriptEventCallback = (
event,
{ environment }: HubSpotRuntimeContext,
) => {
const scriptLoaderURL = environment.spaceInstallation.configuration.script_loader_url;
if (!scriptLoaderURL) {
throw new Error(
`The HubSpot Script Loader URL is missing from the configuration (ID: ${event.spaceId}).`,
);
}

return new Response(script.replace('<TO_REPLACE_SCRIPT_LOADER_URL>', scriptLoaderURL), {
headers: {
'Content-Type': 'application/javascript',
'Cache-Control': 'max-age=604800',
},
});
};

export default createIntegration<HubSpotRuntimeContext>({
fetch_published_script: handleFetchEvent,
});
15 changes: 15 additions & 0 deletions integrations/hubspot/src/script.raw.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
(function (h, u, b, s, p, o, t) {
if (!u.getElementById(b)) {
const scriptLoaderURL = '<TO_REPLACE_SCRIPT_LOADER_URL>';
s = u.getElementsByTagName('head')[0];
p = u.createElement('script');
p.src = scriptLoaderURL;
p.type = 'text/javascript';
p.id = b;
p.async = 1;
p.defer = 1;
s.appendChild(p);
o = h._hsp = h._hsp || [];
o.push(['setContentType', 'knowledge-article']);
}
})(window, document, 'hs-script-loader');
3 changes: 3 additions & 0 deletions integrations/hubspot/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "@gitbook/tsconfig/integration.json"
}