WordPress taxonomy listing page – access post count in Google Tag Manager
The plugin can add the number of posts shown on the current page and the total number of posts behind the current listing to the data layer. Both variables are most useful on category, tag and custom taxonomy listing pages, where they tell you how much content the visitor was offered.
Both variables come from a single option called Post count, in the Post data group of the Page variables section on the plugin settings screen. The option is switched off by default, so if the two variables are missing from your data layer, enable it first. Once it is enabled the plugin adds both variables to every page of your site and not only to listing pages, so check pagePostType2 in your Google Tag Manager triggers when you want to act on listing pages only.
This page describes GTM4WP 2.0.0 and newer. Options introduced by that release are marked New in 2.0.0 and are not available in the legacy 1.x versions. Where 1.x behaved differently, the older behavior is marked with a bold GTM4WP 1.x: lead-in.
GTM4WP 1.x: the same option lived in the Basic data tab of the settings screen.
postCountOnPage
The number of posts that WordPress has loaded for the page currently being viewed, as a number. On a listing page it is at most the number of posts per page set in the reading settings of WordPress, and it is lower than that on the last page of a listing. Themes and plugins can set a different number of posts per page for an individual archive, so treat the reading setting as the usual limit rather than a guaranteed one.
On a single post or page the value is 1. On a search results page it holds the number of results shown on the current page, the same value as the siteSearchResults variable described on the site search page.
postCountTotal
The total number of posts that match the current listing, counted across all of its pages, as a number. On a category, tag or custom taxonomy page it is the number of published posts assigned to that term.
Use it to find empty listing pages. A page where postCountTotal is 0 gave the visitor nothing to read, which usually means a term whose posts have all been unpublished, or a term that is linked in a menu or a widget but has no content yet. Create a Google Tag Manager trigger that fires when the value is 0 and send an event to your analytics tool, so that you get a report of these pages and can make sure every piece of content that has to be public is reachable.
When the main WordPress query is not available, for example because another plugin has replaced it before the data layer is built, both variables are left out of the data layer instead of being added with a value of 0. Build your triggers so that they cope with a variable that is not present.
GTM4WP 1.x: the variables reported 0 in this situation, which could not be told apart from a listing page that is genuinely empty.
Other options that add data to listing pages
The following options sit in the same section of the settings screen and describe the listing itself rather than counting its posts:
- Posttype of current post/archive, enabled by default, adds
pagePostTypeandpagePostType2. On a listing pagepagePostType2carries a prefix that names the kind of listing:category-poston a category page,tag-poston a tag page, andtax-followed by the post type on a custom taxonomy page. This is the variable to use when you want a tag to fire on listing pages only. - Category list of current post/archive, enabled by default, adds
pageCategoryon category and custom taxonomy pages. Tag listing pages do not receive this variable. - New in 2.0.0: Include parent categories in the category list adds the ancestors of each category to the same
pageCategoryarray, so that a listing page deep in a category tree also reports the sections above it. It requires the category list option, and it is switched off by default. - Post author name, enabled by default, and Post author ID add
pagePostAuthorandpagePostAuthorIDon author listing pages as well as on single posts. - Post date adds the matching part of the date on date based listing pages. A year archive receives
pagePostDateYear, a month archive also receivespagePostDateMonth, and day archives additionally receivepagePostDateandpagePostDateDay.
Related pages
The variables listed in the previous section are documented in detail on the page and post attributes page. See also site search, user and visitor data and multisite information.
