Tracking an iframe and setting it's parent page as the Referrer URL


#1

My users can embed an iframe from my site on their sites. I have Piwik working well and it tracks each time the iframe is loaded, but I really need to know which website is loading it.

Is it possible to set the Referrer to the page that loaded the iframe? I’m using the js tracking API (not the php one) and all Piwik code is inside my iframe.

I was hoping I would be able to do something like this from within the iframe:

var _paq = _paq || [];
_paq.push(["setReferrerUrl", document.referrer]);
_paq.push(["trackPageView"]);

Is there any way to do what I want?


#2

I solved this by using the tracking pixel inside our iframe:

(function() {
  var d=document, 
      i=d.createElement("img"), 
      b=d.getElementsByTagName("body")[0],
      r=d.referrer,   
      u="https://mysite.piwik.pro/piwik.php?idsite=3&rec=1&url=myurl&urlref=";
  // add document.referrer (the url of the iframe's parent page) to the end of the string
  i.src=u+r;
  b.appendChild(i);
})();

I’m using JS to append the img tag to the body, so that I can set the urlref param to ‘document.referrer’. Now I am able to track which sites my iframe is being viewed on the most. Works fine.


(Matthieu Aubry) #3

I think your first idea: _paq.push([“setReferrerUrl”, document.referrer]);

should have worked as well? cheers