Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Track Clicks in an IFrame with Google Analytics?

I'd like to track outbound clicks within an iframe on my self-hosted Wordpress site. I already use Google Analytics to track inbound visitors (GA code in the header), but I'd like to know which/how many of those visitors click on links within the iframe on each of my pages. For the record, the iframe does come from an external advertiser.

This is the iframe code I was given (using sample names in place of real ones):

<iframe src="http://www.advertisersite123.com//widget.html?width=510&height=950&product=2" frameborder="0" scrolling="no" width="510" height="950"></iframe>

This line of code is currently pasted on single.php, making a table of links appear on every single post at the bottom of the page.

I'd like to know how many clicks (just clicks in general is fine, don't need to know where they go) are occurring in this iframe. Ideally broken down by each individual URL on my website so I can determine which URLs are more successful in generating clicks within the iframe.

And if possible, I'd like to create a funnel that tells me which visitors to my website (by source) exit via clicks in the iframe.

Is any of this possible? And if so, what code do I add to the page?

Thank you for any and all assistance, it is appreciated!

like image 653
Colin Rock Avatar asked Sep 23 '13 19:09

Colin Rock


Video Answer


1 Answers

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.

like image 184
Crayon Violent Avatar answered Sep 28 '22 03:09

Crayon Violent