Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,15 @@
);
});

it('accepts anonymizeIP', () => {
it('rejects anonymizeIP', () => {
const config: Options = {
...MinimalConfig,
// @ts-expect-error: removed option
anonymizeIP: true,
};
expect(testValidateOptions(config)).toEqual(validationResult(config));
expect(() => testValidateOptions(config)).toThrowErrorMatchingInlineSnapshot(
`"The "anonymizeIP" option is no longer needed and has been removed. In Google Analytics 4, IP addresses are not logged or stored, making IP anonymization unnecessary. See https://support.google.com/analytics/answer/2763052"`,

Check failure on line 106 in packages/docusaurus-plugin-google-gtag/src/__tests__/options.test.ts

View workflow job for this annotation

GitHub Actions / Lint

Unknown word (anonymization) -- or stored, making IP anonymization unnecessary. See https
);
});

it('accepts single trackingID', () => {
Expand Down
17 changes: 3 additions & 14 deletions packages/docusaurus-plugin-google-gtag/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,14 @@
import type {LoadContext, Plugin} from '@docusaurus/types';
import type {PluginOptions, Options} from './options';

function createConfigSnippet({
trackingID,
anonymizeIP,
}: {
trackingID: string;
anonymizeIP: boolean;
}): string {
return `gtag('config', '${trackingID}', { ${
anonymizeIP ? "'anonymize_ip': true" : ''
} });`;
function createConfigSnippet(trackingID: string): string {
return `gtag('config', '${trackingID}');`;
}

function createConfigSnippets({
trackingID: trackingIDArray,
anonymizeIP,
}: PluginOptions): string {
return trackingIDArray
.map((trackingID) => createConfigSnippet({trackingID, anonymizeIP}))
.join('\n');
return trackingIDArray.map(createConfigSnippet).join('\n');
}

export default function pluginGoogleGtag(
Expand Down
16 changes: 6 additions & 10 deletions packages/docusaurus-plugin-google-gtag/src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,13 @@

export type PluginOptions = {
trackingID: [string, ...string[]];
// TODO deprecate anonymizeIP after June 2023
// "In Google Analytics 4, IP masking is not necessary
// since IP addresses are not logged or stored."
// https://support.google.com/analytics/answer/2763052?hl=en
anonymizeIP: boolean;
};

export type Options = {
trackingID: string | [string, ...string[]];
anonymizeIP?: boolean;
};

export const DEFAULT_OPTIONS: Partial<PluginOptions> = {
anonymizeIP: false,
};
export const DEFAULT_OPTIONS: Partial<PluginOptions> = {};

const pluginOptionsSchema = Joi.object<PluginOptions>({
// We normalize trackingID as a string[]
Expand All @@ -39,7 +31,11 @@
Joi.array().items(Joi.string().required()),
)
.required(),
anonymizeIP: Joi.boolean().default(DEFAULT_OPTIONS.anonymizeIP),
// @ts-expect-error: removed option
anonymizeIP: Joi.any().forbidden().messages({
'any.unknown':
'The "anonymizeIP" option is no longer needed and has been removed. In Google Analytics 4, IP addresses are not logged or stored, making IP anonymization unnecessary. See https://support.google.com/analytics/answer/2763052',

Check failure on line 37 in packages/docusaurus-plugin-google-gtag/src/options.ts

View workflow job for this annotation

GitHub Actions / Lint

Unknown word (anonymization) -- or stored, making IP anonymization unnecessary. See https
}),
});

export function validateOptions({
Expand Down
Loading