This GatsbyJS plugin allows to define tags that should be inserted in the head or body
of a page. Using gatsby-plugin-tagmanager
you can add any thrird party tool with ease to your site
and do not need to edit the default-html.js directly or create plugins just to add your tools.
By following a few simple rules gatsby-plugin-tagmanager
is a powerful helper to speed
up your develoment process.
The installation of gatsby-plugin-tagmanager
is done the same way as you would install other plugins.
Refer to Using a plugin in your site for a detailed guide.
Note that you need npm
to use this plugin. You might also use another prefered package manager that you currently use.
Run the following commandto install gatsby-plugin-tagmanager
:
npm install gatsby-plugin-tagmanager
Put the plugin into your gatsby-config.js
. The order of the plugins in your gatsby-config.js
specifies
their load order. If you want your tags to be at the top of all other plugins that insert tags, put gatsby-plugin-tagmanager
at the top of your plugin list and vice versa.
module.exports = {
plugins: [
...
{
`gatsby-plugin-tagmanager`,
options: {
...
}
},
...
]
};
Can be used to add tags to the head of the generated pages.
The value is an array that contains JSONs in the format specified under Tag configuration
which is a sub heading of Available options
.
Example value: [{...tag in the defined format}, {...tag in the defined format},]
Can be used to add tags to the start of the body of the generated pages.
The value is an array that contains JSONs in the format specified under Tag configuration
which is a sub heading of Available options
.
Example value: [{...tag in the defined format}, {...tag in the defined format},]
Can be used to add tags to the end of the body of the generated pages.
The value is an array that contains JSONs in the format specified under Tag configuration
which is a sub heading of Available options
.
Example value: [{...tag in the defined format}, {...tag in the defined format},]
The type of the tag. This can be something like link
or script
for example.
Example value: "link"
A internal name that can be set. Has to be unique and can collide with other values on the page
if a wrong value is set. This value is used as the key
of the React component.
Example value: "my-script"
The attributes that should be set on the tag. Has to be in JSON format. Can be used for things like defining the type of a script and other common HTML attributes.
Example value:
{
type: "text/css",
rel: "stylesheet"
}
The content to put into the tag. Note that this content is being escaped properly and is safe to use.
Note: Cannot be used to insert JavaScript into an script tag. You should use dangerousContent
for that.
Example value: "Hello World!"
Allows to insert some custom HTML as content for an tag. Can be used for example to include some external JavaScript snippet.
Note that if dangerousContent
and content
is set, content
is the property that will be used and overwrite this property.
Example value: "console.log('Hello World!');"
{
resolve: "gatsby-plugin-tagmanager",
options: {
head: [
{
type: "link",
name: "some-css",
attributes: {
type: "text/javascript",
"data-somedata": "MyData",
href: "/my.css"
},
}
]
}
},
{
resolve: "gatsby-plugin-tagmanager",
options: {
preBody: [
{
type: "h1",
name: "some-h1",
content: "This is my website!"
}
]
}
},
{
resolve: "../gatsby-plugin-tagmanager",
options: {
preBody: [
{
type: "script",
name: "custom-script",
attributes: {
type: "text/javascript"
}
dangerousContent: "console.log('Hello World');"
}
]
}
},
To develop locally you need the following tools:
- NodeJS (recommended version: 14.15.4)
- NPM
To set the project up, simply let npm install your dependencies as always:
npm install
To build the project use the build script provided by npm:
npm run build
Note that building will also run eslint and jest tests.
If you want to collect coverage while building, use the following script instead:
npm run buildCoverage
To compile the project run the following npm script:
npm run compile
To clean the compiled output (which lays in the root directory), run:
npm run clean
The project uses eslint as linter and jest as the testing framework. You can run the tools using npm.
Use the following npm command to run eslint and jest:
npm run test
Use the following npm command to run eslint:
npm run lint
Use the following npm command to run jest:
npm run test
You are welcome to contribute to gatsby-plugin-tagmanager
! Refer to Contributing for information about issues and code contributions.