Google Tag Manager template tags and settings tool. Now supports Server Side GTM.
There are some breaking changes in v3.0.0:
- data layer no longer passes
post.author_id
by default - data layer
post.ID
renamed topost.id
- data layer
term.ID
renamed toterm.id
- data layer
author.ID
renamed toauthor.id
- data layer
user.role
is no longer an array but a comma separated list of roles - data layer
post.<taxonomy>
is no longer an array but a comma separated list of term names - data layer
post.author_slug
is no longer available by default, usepost.author_name
instead
Once the plugin is installed and activated there are 2 places you can configure:
- On the general settings page in admin add your container ID eg.
GTM-123ABC
- For a multisite install you can set a network wide container ID on the network settings screen
You can optionally specify the following:
- Server container URL - this should the absolute path to the server container, even if you are using a reverse proxy.
- Custom code snippet - this is a block of code that will be added to the
<head>
of the page. Some server side container providers have subtle or extensive changes to the standard GTM snippet so you can override it fully if needed. - Cookie preservation - you can set a special UUID cookie that lasts a long time, some server side providers allow restoring cookies set by 3rd parties or client side code in this way.
NOTE: This is not needed for block themes.
If your theme is a classic theme, and you wish to support the fallback iframe for devices without javascript, add the following code just after the opening <body>
tag in your theme:
do_action( 'wp_body_open' );
GTM offers a dataLayer
which allows you pass arbitrary data that can be used to modify which tags are added to your site.
This plugin adds some default information such as page author, tags and categories and provides a simple filter for adding in your own custom key/value pairs.
<?php
add_filter( 'hm_gtm_data_layer', function( $data ) {
$data['my_var'] = 'hello';
return $data;
} );
?>
Find out more about using the dataLayer
variable here.
You can explore and view the dataLayer
variables by previewing your container and using the overlay on your website.
By default the plugin will look for elements with special data attributes in your markup and listen to the specified event to push events to the data layer.
The data attributes are:
data-gtm-on
: enum [click|submit|keyup|focus|blur] The JS event to listen for, defaults to 'click'.data-gtm-event
: string The name or action of the event eg. "play".data-gtm-category
: string Optional group the event belongs to.data-gtm-label
: string Optional human readable label for the event.data-gtm-value
: number Optional numeric value associated with the event eg. product price.data-gtm-fields
: string Optional extra data provided as encoded JSON.data-gtm-var
: string Optionally override the default dataLayer variable name for this event.
Example:
<button
data-gtm-on="click"
data-gtm-event="play"
data-gtm-category="videos"
data-gtm-label="Featured Promotional Video"
>
Play video
</button>
There is also a helper function to return these data attributes called get_gtm_data_attributes()
.
To deactivate custom event tracking use the following code:
add_filter( 'hm_gtm_enable_event_tracking', '__return_false' );