Setting a page as a blog post or regular page

I have been asked a few times to count blog traffic differently from traffic to other parts of the site. Presumably, this is to see if the blog actually does anything in the conversion funnel.

For most WordPress sites, the blog is in its own directory, so its easy. You can just set up a trigger on page view with the condition, page path contains ‘blog'.

However, there are some permalink structures that put everything in the root, so its not easy to identify which pages are pages and which ones are posts. For example, this site has no /blog/ structure.

My first thought was to view the source and see if there is anything in the code that differentiates a blog and a page. I found

@type”:”BlogPosting”
@type”:”Article”

in some schema that RankMath SEO was producing. If only I could parse this or test if it contained ‘BlogPosting'

So I found the solution here on stack overflow. Well a similar one with Yoast SEO. Basically, the problem is how to parse JSON with Javascript.

function() {
  var publishedDate = "";
  var yoastInfo = JSON.parse(document.head.querySelector('.yoast-schema-graph.yoast-schema-graph--main').innerHTML); 
  vars = yoastInfo["@graph"];
  for(var i in vars) {
    if(vars[i]['@type'] === 'WebPage') {
      publishedDate=(vars[i]['datePublished']); 
    }
  }



  return publishedDate;
}

I managed to modify that function to work with Rank Math and check if any of the @types are BlogPostings.

function() {
  var blog = false;
  var Info = JSON.parse(document.head.querySelector('.rank-math-schema').innerHTML); 
  vars = Info["@graph"];
  for(var i in vars) {
    if(vars[i]['@type'] === 'BlogPosting') {
      blog = true; 
    }
  }



  return blog;
}

So that returns true for any page that is a blog post.

The Plugin Way

There is a plugin called Google Tag Manager for WordPress. It populates the data layer with the post type and you can grab that in tag manager.

You can create an event called viewed_blog when the post type is blog. Its all straightforward to do. If you view the source to this page and look for datalayer,

var dataLayer_content={“pagePostType”:”post”,”pagePostType2″:”single-post”,”pageCategory”:[“tips”],”pagePostAuthor”:”Ben”}

you'll get something like that. Just create a custom variable that takes the value of the “pagePostType”.

Similar Posts

  • Getting data into BigQuery

    I’m a big believer in serendipity. I connected with the founder of Weavely.io. It just so happens I’m going to be developing a machine learning model to predict the 1-year value of a customer based on their first purchase. One of the big challenges is getting all the data into…

  • ICO Vat Receipts

    All companies that handle external data have to register with the Information Commissioners Office (ICO). This costs £35 a year for most organisations. For large organisations its £500 but most people won’t need to worry about that. The costs and registration process are pretty reasonable and straightforward. The problem comes…

  • Featured on Digital EATS

    I recently was featured on “Digital E-A-T’s”, a unique platform that celebrates the fantastic people in the digital industry. The site is the brainchild of Mike Ginley, a Sr. SEO Specialist at Humana in Chicago, IL, who has created this space to highlight individuals known for their contributions, promote those…

  • | |

    AI mode

    I saw that with a VPN you can actually test out AI mode now, so I thought I’d give it a whirl. I asked it, “Who’s the best PPC person?” It came with a load of agencies, and then asked about me. This is a problem because in this AI…

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *