Google AMP and Instant Articles

For Instant Articles I’m trying to use this script:

<!-- Piwik -->
<script>
  var _paq = _paq || [];
  _paq.push(['setCookieDomain', '*.xxx']);
  _paq.push(['setDomains', ['*.xxx']]);
  _paq.push(['setDocumentTitle', ia_document.title]);
  _paq.push(['setCustomUrl', ia_document.shareURL]);
  _paq.push(['setReferrerUrl', ia_document.referrer]);
  _paq.push(['setCampaignNameKey', 'InstantArticles']);
  _paq.push(['enableHeartBeatTimer', 5]);
  _paq.push(['trackAllContentImpressions']);
  _paq.push(['enableLinkTracking']);
  _paq.push(['trackPageView']);
  (function() {
    var u="//xxx/";
    _paq.push(['setTrackerUrl', u+'piwik.php']);
    _paq.push(['setSiteId', 2]);
    var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
    g.type='text/javascript'; g.async=true; g.defer=true; g.src=u+'piwik.js'; s.parentNode.insertBefore(g,s);
  })();
</script>
<!-- End Piwik Code -->

Based on what I’ve found at Analytics - Instant Articles - Documentation - Facebook for Developers
ia_document.title is the title, ia_document.shareURL is the URL and ia_document.referrer is fixed to “ia. facebook .com”.

It’s working fine but I’m not getting the ReferrerUrl and CampaigName in my piwik stats. If anyone has some suggestion on how to improve my script I would really appreciate :wink:

Tip: you can test the analytics using www.ia-tracker.fbsbx.com/instant_article_test?url=[article-url] without need to open on mobile phone

I used similar code for Google Analytics and it works fine:

<!-- GoogleAnalytics -->
<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','https://www.google-analytics.com/analytics.js','ga');
  ga('create', 'UA-XXXXXXXX-2', 'auto');
  ga('set', 'anonymizeIp', true);
  ga('set', 'campaignName', 'Instant Articles');
  ga('set', 'campaignSource', 'Facebook');
  ga('set', 'title', ia_document.title);
  ga('set', 'referrer', ia_document.referrer);
  ga('set', 'location', ia_document.shareURL);
  ga('send', 'pageview');
</script>
<!-- End GoogleAnalytics -->

Thanks for your help

Regards
Fabio

1 Like