Iframe Cross Domain Tracking With GTM

Here I love to share the easiest way of GTM iframe tracking that I found ever. So, borrowing some code from GTM expert that everyone know – Simo Ahava.

I tested this below condition and was working well.

So, into the topic. Basically, we need only a CustomJs GTM variable with hitcall back function and rest is magic.  😀

Step I

Create a customJs variable named callBack

function() {
  return function() {
    try { 
      var gobj = window[window.GoogleAnalyticsObject];
      var iframe = document.querySelector('#myIframe');
      var tracker, linker;
      if (gobj) {
        tracker = gobj.getAll()[0];
        linker = new window.gaplugins.Linker(tracker);
        iframe.src = linker.decorate(iframe.src);
      }
    } catch(e) {}
  }
}

Please refer – Orginal Blog Post for code explained.

Step II

Now, at the Universal Analytics tag in GTM  >> MoreSettings > Fields to set >> the add :

hitcallback with variable {{callback}

 

Done!  like magic, your iframe link will be decorated. To check if its working use RealTime Analytics or Inspect element to find cid appended to iframe.

 

Read more