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

Google Analytics updated configuration #561

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
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/slow-keys-confess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@gitbook/integration-googleanalytics': minor
---

Update Google Analytics integration configuration
10 changes: 1 addition & 9 deletions integrations/googleanalytics/gitbook-manifest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ script: ./src/index.ts
# The following scope(s) are available only to GitBook Staff
# See https://developer.gitbook.com/integrations/configurations#scopes
scopes:
- space:script:inject
- space:script:cookies
- site:script:inject
- site:script:cookies
contentSecurityPolicy:
Expand Down Expand Up @@ -53,10 +51,4 @@ categories:
- analytics
configurations:
site:
properties:
tracking_id:
type: string
title: Tracking ID
description: Look for this in your Google Analytics account.
required:
- tracking_id
componentId: config
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious: why are we replacing the tracking id with a component id?

Could we add a title and description to it to clarify its role and use?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have a look here for more context.

We're replacing the way we render the configuration. From controlled by declarative + controlled by gitbook-x to controlled by the integration completely.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This as well.

83 changes: 83 additions & 0 deletions integrations/googleanalytics/src/components.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { createComponent, RuntimeContext, RuntimeEnvironment } from '@gitbook/runtime';

type GARuntimeEnvironment = RuntimeEnvironment<{}, GASiteInstallationConfiguration>;

type GARuntimeContext = RuntimeContext<GARuntimeEnvironment>;

type GASiteInstallationConfiguration = {
tracking_id?: string;
};

type GAState = GASiteInstallationConfiguration;

type GAProps = {
siteInstallation: {
configuration?: GASiteInstallationConfiguration;
};
};

export type GAAction = { action: 'save.config' };

export const configBlock = createComponent<GAProps, GAState, GAAction, GARuntimeContext>({
componentId: 'config',
initialState: (props) => {
const siteInstallation = props.siteInstallation;
return {
tracking_id: siteInstallation.configuration?.tracking_id ?? '',
};
},
action: async (element, action, context) => {
switch (action.action) {
case 'save.config':
const { api, environment } = context;
const siteInstallation = environment.siteInstallation;
if (!siteInstallation) {
throw Error('No site installation found');
}

const configurationBody = {
...siteInstallation.configuration,
tracking_id: element.state.tracking_id,
};
await api.integrations.updateIntegrationSiteInstallation(
siteInstallation.integration,
siteInstallation.installation,
siteInstallation.site,
{
configuration: {
...configurationBody,
},
},
);
return element;
}
},
render: async () => {
return (
<configuration>
<input
label="Client ID"
hint={<text>The unique identifier of your GA app client.</text>}
element={<textinput state="tracking_id" placeholder="Tracking ID" />}
/>

<divider />
<input
label=""
hint=""
element={
<button
style="primary"
disabled={false}
label="Save"
tooltip="Save configuration"
onPress={{
action: 'save.config',
}}
/>
}
/>
</configuration>
);
},
});
2 changes: 2 additions & 0 deletions integrations/googleanalytics/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
RuntimeEnvironment,
} from '@gitbook/runtime';

import { configBlock } from './components';
import script from './script.raw.js';

type GARuntimeContext = RuntimeContext<
Expand Down Expand Up @@ -35,4 +36,5 @@ export const handleFetchEvent: FetchPublishScriptEventCallback = async (

export default createIntegration<GARuntimeContext>({
fetch_published_script: handleFetchEvent,
components: [configBlock],
});