Use WordPress, browser and device data in Google Tag Manager
Track WordPress post and page attributes, including custom post types. Use WordPress data for content groups and user segments, and add the visitor’s browser, operating system, device and country to the data layer.
This page describes GTM4WP 2.1.0. Where the legacy 1.x versions behaved differently, the older behavior is marked with a bold GTM4WP 1.x: lead-in.
How the data gets into the data layer
GTM4WP collects the values you switch on while WordPress builds the page, and writes them into the page as a single data layer push. That push sits in a script placed before the Google Tag Manager container code and carries no event name, so the values are already in the data layer when Google Tag Manager starts. Every tag can read them, including tags fired by a Page View trigger.
Two groups of values arrive a moment later, each in a data layer event of its own:
- Browser, operating system and device data, in the
gtm4wp.deviceDataevent. The browser collects these values after the page has loaded. - Visitor data and the site search term, in the
gtm4wp.visitorDataevent, but only while the Cache-safe data layer option is switched on. That option is off by default.
A tag that reads one of these values needs a Custom Event trigger on that event instead of a Page View trigger. The trigger setup page covers both events, together with the customer and cart events of an online store.
GTM4WP 1.x: browser, operating system and device data was detected on the server and was part of the first data layer push.
The plugin writes into the dataLayer variable, the name Google Tag Manager uses by default. If your site needs another name, set it in the dataLayer variable name option of the container settings.
Switching variables on
Every variable, or small group of variables, has its own option, so the data layer carries only what you ask for. Only four options are on by default: the post type, the categories and the tags of the current post or archive, and the name of the post author. Everything else stays off until you switch it on.
The options sit in two sections of the plugin settings. The Page variables section groups them as Post data, Content & engagement data, Search data, Visitor data and Site data. Browser, operating system and device data have a section of their own, Browser, OS & device data. The pages below document every variable, grouped the same way:
| Settings group | Documented on |
|---|---|
| Post data, Content & engagement data | Page / post attributes; the post count variables on Page / post listings |
| Search data | Site search |
| Visitor data | User and visitor data; the Cloudflare country code on Geo data |
| Site data | Multisite |
| Browser, OS & device data | Browser data, Operating system data, Device data |
Reading a value in Google Tag Manager
In Google Tag Manager, open Variables, click New in the User-Defined Variables section and choose the Data Layer Variable type. Enter the data layer key exactly as these pages write it, for example pagePostType. Keys are case sensitive.
A value stays readable after the event that brought it. Google Tag Manager keeps every key it has received during the page view, so a tag fired on gtm4wp.deviceData can still read pagePostType from the first push.
What visitors can see
The data layer is part of the public page. Anyone can read it in the page source or in the browser console, so every value you switch on is visible to every visitor of that page.
That is why the options that describe a logged in visitor, such as the user name, the email address or the IP address, are off by default. The post custom fields option deserves the same care: it publishes every custom field of the post whose name does not start with an underscore, unless you list the ones you need.
On a site with full page caching, a value written into the page for one visitor is also served to the next one. The Cache-safe data layer option keeps visitor values out of the cached HTML, as the trigger setup page explains. For everything GTM4WP stores and sends, see the GDPR page.
Adding your own values
Developers can add, change or remove values with the gtm4wp_compile_datalayer filter, described on the actions and filters page.
WordPress data
These pages describe the values WordPress itself knows: the content being viewed, the site search, the visitor and the site.
Page / Post attributes
Use post ID, author name, category and tag list to track content and analyze performance.
Page / Post listings
Use the total number of posts in a taxonomy listing or the items shown on a single page.
Site search
Track what the visitor searched for, where the search came from and how many results were shown.
Visitor data
Track logged in status, user role and user ID to tailor tracking and reporting to specific user groups.
Multisite
GTM4WP works on WordPress Multisite with some limitations. Learn more about the opportunities.
Browser, device and geo data
These pages describe the values that come from the visitor’s browser, and the country code Cloudflare adds to each request.
Device data
Track the type of device, such as desktop or mobile, and its model where the browser reports it.

