3

I am running a WordPress which using Automattic/facebook-instant-articles-wp. But I realise the traffic tracked for IA showed in GA were unable to show the title.

A lot of website is recommending this for the GA code.

<script> (function (i,s,o,g,r,a,m) {i['GoogleAnalyticsObject']=r;i[r]=i[r]||function () {(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)})(window,document,'script','//www.google- analytics.com/analytics.js','ga'); ga('create', 'ANALYTICS ID', 'auto'); ga('require', 'displayfeatures'); ga('set', 'campaignSource', 'Facebook'); ga('set', 'campaignMedium', 'Social Instant Article'); ga('send', 'pageview', {title: 'POST TITLE'}); </script>

Then it end up showing 'POST TITLE' in GA. Anyone have any clue to show the article title?

Thank you.

Carson Lee
  • 2,673
  • 3
  • 20
  • 23

4 Answers4

2

The code that you've posted ends with:

ga('send', 'pageview', {title: 'POST TITLE'});

This is why 'POST TITLE' is coming up in GA. By editing this field, you can decide how it will be reported to GA.

J Brazier
  • 864
  • 6
  • 14
2

There are a few answers that might be applicable here but I will show what requires the least modification here:

in your header:

<html <?php language_attributes(); ?>  data-title="<?php echo get_the_title();?>">

You will need to replace your own logic for get_the_title() some pages created by use of rewrites will not have an title. But for most usage it will suffice.

<script>

            (function (i,s,o,g,r,a,m) {i['GoogleAnalyticsObject']=r;i[r]=i[r]||function () {(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),                         m=s.getElementsByTagName(o)0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)})(window,document,'script','//www.google-   analytics.com/analytics.js','ga');
            ga('create', 'ANALYTICS ID', 'auto');
            ga('require', 'displayfeatures');
            ga('set', 'campaignSource', 'Facebook');
            ga('set', 'campaignMedium', 'Social Instant Article');
            ga('send', 'pageview', {title: document.documentElement.getAttribute('data-title')});
</script>
David
  • 5,897
  • 3
  • 24
  • 43
2

Since you're using embed code and not sure what could be POST TITLE, you should try document.title:

ga('send', {hitType: 'pageview', title: document.title});

See page tracking for more info. Remember to change ANALYTICS ID to actual ID too.

wpclevel
  • 2,451
  • 3
  • 14
  • 33
2

GA code like this should give you correct titles:

<script>
 ...
ga('create', 'XX-XXXXXXXXX-X', 'auto');
ga('require', 'displayfeatures');
ga('set', 'campaignSource', 'Facebook');
ga('set', 'campaignMedium', 'Social Instant Article');
ga('set', 'title', 'IA - '+ia_document.title); // get your title 
ga('send', 'pageview');
</script>

So with ia_document.title you can get article title correct in analytics.

Here is a Facebook's reference to the issue: https://developers.facebook.com/docs/instant-articles/analytics#analytics-services

Hope this helps.