-
-
Notifications
You must be signed in to change notification settings - Fork 8.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(plugin-google-tag-manager): add new google-tag-manager plugin + …
…deprecate google-analytics plugin (#8470) Co-authored-by: Goolsby, Lane <[email protected]> Co-authored-by: sebastienlorber <[email protected]> fix #7221
- Loading branch information
1 parent
e1d6292
commit 428af8a
Showing
14 changed files
with
264 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.tsbuildinfo* | ||
tsconfig* | ||
__tests__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# `@docusaurus/plugin-google-tag-manager` | ||
|
||
Google Tag Manager plugin for Docusaurus. | ||
|
||
## Usage | ||
|
||
See [plugin-google-tag-manager documentation](https://docusaurus.io/docs/api/plugins/@docusaurus/plugin-google-tag-manager). |
33 changes: 33 additions & 0 deletions
33
packages/docusaurus-plugin-google-tag-manager/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
{ | ||
"name": "@docusaurus/plugin-google-tag-manager", | ||
"version": "3.0.0-alpha.0", | ||
"description": "Google Tag Manager (gtm.js) plugin for Docusaurus.", | ||
"main": "lib/index.js", | ||
"types": "lib/index.d.ts", | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"scripts": { | ||
"build": "tsc --build", | ||
"watch": "tsc --build --watch" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/facebook/docusaurus.git", | ||
"directory": "packages/docusaurus-plugin-google-tag-manager" | ||
}, | ||
"license": "MIT", | ||
"dependencies": { | ||
"@docusaurus/core": "^3.0.0-alpha.0", | ||
"@docusaurus/types": "^3.0.0-alpha.0", | ||
"@docusaurus/utils-validation": "^3.0.0-alpha.0", | ||
"tslib": "^2.4.0" | ||
}, | ||
"peerDependencies": { | ||
"react": "^16.8.4 || ^17.0.0", | ||
"react-dom": "^16.8.4 || ^17.0.0" | ||
}, | ||
"engines": { | ||
"node": ">=16.14" | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
packages/docusaurus-plugin-google-tag-manager/src/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import {Joi} from '@docusaurus/utils-validation'; | ||
import type { | ||
LoadContext, | ||
Plugin, | ||
OptionValidationContext, | ||
} from '@docusaurus/types'; | ||
import type {PluginOptions, Options} from './options'; | ||
|
||
export default function pluginGoogleAnalytics( | ||
context: LoadContext, | ||
options: PluginOptions, | ||
): Plugin { | ||
const {containerId} = options; | ||
const isProd = process.env.NODE_ENV === 'production'; | ||
|
||
return { | ||
name: 'docusaurus-plugin-google-tag-manager', | ||
|
||
contentLoaded({actions}) { | ||
actions.setGlobalData(options); | ||
}, | ||
|
||
injectHtmlTags() { | ||
if (!isProd) { | ||
return {}; | ||
} | ||
return { | ||
preBodyTags: [ | ||
{ | ||
tagName: 'noscript', | ||
innerHTML: `<iframe src="https://www.googletagmanager.com/ns.html?id=${containerId}" height="0" width="0" style="display:none;visibility:hidden"></iframe>`, | ||
}, | ||
], | ||
headTags: [ | ||
{ | ||
tagName: 'link', | ||
attributes: { | ||
rel: 'preconnect', | ||
href: 'https://www.googletagmanager.com', | ||
}, | ||
}, | ||
{ | ||
tagName: 'script', | ||
innerHTML: `window.dataLayer = window.dataLayer || [];`, | ||
}, | ||
{ | ||
tagName: 'script', | ||
innerHTML: `(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start': | ||
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0], | ||
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src= | ||
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f); | ||
})(window,document,'script','dataLayer','${containerId}');`, | ||
}, | ||
], | ||
}; | ||
}, | ||
}; | ||
} | ||
|
||
const pluginOptionsSchema = Joi.object<PluginOptions>({ | ||
containerId: Joi.string().required(), | ||
}); | ||
|
||
export function validateOptions({ | ||
validate, | ||
options, | ||
}: OptionValidationContext<Options, PluginOptions>): PluginOptions { | ||
return validate(pluginOptionsSchema, options); | ||
} | ||
|
||
export type {PluginOptions, Options}; |
12 changes: 12 additions & 0 deletions
12
packages/docusaurus-plugin-google-tag-manager/src/options.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
export type PluginOptions = { | ||
containerId: string; | ||
}; | ||
|
||
export type Options = Partial<PluginOptions>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
/// <reference types="@docusaurus/module-type-aliases" /> |
15 changes: 15 additions & 0 deletions
15
packages/docusaurus-plugin-google-tag-manager/tsconfig.client.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"noEmit": false, | ||
"composite": true, | ||
"incremental": true, | ||
"tsBuildInfoFile": "./lib/.tsbuildinfo-client", | ||
"module": "esnext", | ||
"target": "esnext", | ||
"rootDir": "src", | ||
"outDir": "lib" | ||
}, | ||
"include": ["src/*.d.ts"], | ||
"exclude": ["**/__tests__/**"] | ||
} |
13 changes: 13 additions & 0 deletions
13
packages/docusaurus-plugin-google-tag-manager/tsconfig.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"references": [{"path": "./tsconfig.client.json"}], | ||
"compilerOptions": { | ||
"noEmit": false, | ||
"incremental": true, | ||
"tsBuildInfoFile": "./lib/.tsbuildinfo", | ||
"rootDir": "src", | ||
"outDir": "lib" | ||
}, | ||
"include": ["src"], | ||
"exclude": ["**/__tests__/**"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
--- | ||
sidebar_position: 8 | ||
slug: /api/plugins/@docusaurus/plugin-google-tag-manager | ||
--- | ||
|
||
# 📦 plugin-google-tag-manager | ||
|
||
import APITable from '@site/src/components/APITable'; | ||
|
||
A plugin for adding [Google Tag Manager (gtm.js)](https://developers.google.com/tag-platform/tag-manager) to a Docusaurus site. Use this plugin in conjunction with the standard [gtag plugin](./plugin-google-gtag.md) for in-depth analysis of how users are using your site. | ||
|
||
:::tip | ||
|
||
You can use [Google's Tag Assistant](https://tagassistant.google.com/) tool to check if tag manager is set up correctly! | ||
|
||
::: | ||
|
||
:::caution production only | ||
|
||
This plugin is always inactive in development and **only active in production** to avoid polluting the analytics statistics. | ||
|
||
::: | ||
|
||
## Installation {#installation} | ||
|
||
```bash npm2yarn | ||
npm install --save @docusaurus/plugin-google-tag-manager | ||
``` | ||
|
||
:::tip | ||
|
||
If you use the preset `@docusaurus/preset-classic`, you don't need to install this plugin as a dependency. | ||
|
||
You can configure this plugin through the preset options. | ||
|
||
::: | ||
|
||
## Configuration {#configuration} | ||
|
||
Accepted fields: | ||
|
||
```mdx-code-block | ||
<APITable> | ||
``` | ||
|
||
| Name | Type | Default | Description | | ||
| --- | --- | --- | --- | | ||
| `containerId` | `string` | **Required** | Your Tag Manager container Id (usually starts with `GTM-`). | | ||
|
||
```mdx-code-block | ||
</APITable> | ||
``` | ||
|
||
### Example configuration {#ex-config} | ||
|
||
You can configure this plugin through preset options or plugin options. | ||
|
||
:::tip | ||
|
||
Most Docusaurus users configure this plugin through the preset options. | ||
|
||
::: | ||
|
||
```js config-tabs | ||
// Preset Options: googleTagManager | ||
// Plugin Options: @docusaurus/plugin-google-tag-manager | ||
|
||
const config = { | ||
containerId: 'GTM-12345', | ||
}; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters