How to Track Clicks in an IFrame with Google Analytics? How to Track Clicks in an IFrame with Google Analytics? wordpress wordpress

How to Track Clicks in an IFrame with Google Analytics?


What you are asking for is not easily done. In general, you cannot have javascript on the parent page that can hook into events on the iframed page if it is not hosted on the same domain. Same thing for the iframe page trying to tap into the parent page's DOM. This is called cross-site scripting (XSS), which goes against the same domain origin policy.

At a minimum, the easiest thing to do would be to put the GA code on the iframed page. But that's probably not something you can do (though it couldn't hurt to ask the vendor).

But some vendors (usually social media vendors such as Facebook, Google and Twitter) will have their widgets that output an iframe w/ info. They will set their server to allow for cross-domain scripting, which would allow you to tap into the iframe's DOM. Usually though they provide an API along with the widget that makes it easier to hook into relevant events (like share events), so that you don't have to do (much) coding yourself.

Basically long story short, there's no way for you to track it without the person in charge of the iframe domain getting involved.


I was searching for something similar and came across this. This jQuery Plugin lets you track an Iframe.


Tried events?

If the click is out of iframe:

_gaq.push(['_trackEvent', eventCategory, eventAtion, eventLabel]);

If the click is inside the iframe you should use:

window.parent._gaq.push(['_trackEvent', eventCategory, eventAtion, eventLabel]);