diff --git a/.changeset/eight-candles-tie.md b/.changeset/eight-candles-tie.md new file mode 100644 index 000000000..7521cfe2f --- /dev/null +++ b/.changeset/eight-candles-tie.md @@ -0,0 +1,5 @@ +--- +'@gitbook/integration-hubspot': major +--- + +Add HubSpot integration diff --git a/integrations/hubspot/.eslintrc.json b/integrations/hubspot/.eslintrc.json new file mode 100755 index 000000000..2486b4b2d --- /dev/null +++ b/integrations/hubspot/.eslintrc.json @@ -0,0 +1,3 @@ +{ + "extends": ["@gitbook/eslint-config/integration"] +} diff --git a/integrations/hubspot/CHANGELOG.md b/integrations/hubspot/CHANGELOG.md new file mode 100755 index 000000000..c6ba1b3d3 --- /dev/null +++ b/integrations/hubspot/CHANGELOG.md @@ -0,0 +1,9 @@ +# @gitbook/integration-hubspot + +## 1.0.0 + +### Major Changes + +### Minor Changes + +### Patch Changes diff --git a/integrations/hubspot/assets/icon.png b/integrations/hubspot/assets/icon.png new file mode 100644 index 000000000..8b3e29654 Binary files /dev/null and b/integrations/hubspot/assets/icon.png differ diff --git a/integrations/hubspot/gitbook-manifest.yaml b/integrations/hubspot/gitbook-manifest.yaml new file mode 100755 index 000000000..3653ce5b0 --- /dev/null +++ b/integrations/hubspot/gitbook-manifest.yaml @@ -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 diff --git a/integrations/hubspot/package.json b/integrations/hubspot/package.json new file mode 100644 index 000000000..046727050 --- /dev/null +++ b/integrations/hubspot/package.json @@ -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 ." + } +} \ No newline at end of file diff --git a/integrations/hubspot/src/index.ts b/integrations/hubspot/src/index.ts new file mode 100755 index 000000000..9b4ff0a1a --- /dev/null +++ b/integrations/hubspot/src/index.ts @@ -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('', scriptLoaderURL), { + headers: { + 'Content-Type': 'application/javascript', + 'Cache-Control': 'max-age=604800', + }, + }); +}; + +export default createIntegration({ + fetch_published_script: handleFetchEvent, +}); diff --git a/integrations/hubspot/src/script.raw.js b/integrations/hubspot/src/script.raw.js new file mode 100644 index 000000000..236e194b7 --- /dev/null +++ b/integrations/hubspot/src/script.raw.js @@ -0,0 +1,15 @@ +(function (h, u, b, s, p, o, t) { + if (!u.getElementById(b)) { + const scriptLoaderURL = ''; + 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'); diff --git a/integrations/hubspot/tsconfig.json b/integrations/hubspot/tsconfig.json new file mode 100644 index 000000000..f839d1833 --- /dev/null +++ b/integrations/hubspot/tsconfig.json @@ -0,0 +1,3 @@ +{ + "extends": "@gitbook/tsconfig/integration.json" +} \ No newline at end of file