user2484373
user2484373

Reputation: 31

Making a Google AdSense ad to do a function when clicked

Okay, I'm trying to put tags around a Google AdSense ad that will call a function when someone clicks the ad.

I've tried to put tags around it but for some reason it's only taking effect for what's around the ad, not the ad text/picture itself.

This has nothing to do with gaining more clicks or what ever, it's just gonna simply log in a text file how many people have clicked it.

Here's what I've tried so far, and it only takes effect for the space around the ad.

        <div id="ad" onClick="return alert('Hello');">
<script type="text/javascript">
google_ad_client = "ca-pub-0000000000000";
google_ad_slot = "000000000000";
google_ad_width = 250;
google_ad_height = 250;
</script>

<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>

Can someone help me or is this even possible?

Upvotes: 3

Views: 4693

Answers (1)

Steven Moseley
Steven Moseley

Reputation: 16325

AdSense ads are loaded in an iFrame. You can't capture clicks from them by bubbling, due to sandbox security issues. Nor can you trigger clicks on an ad via JavaScript.

What you can do, though, is track page focus to determine whether someone has clicked off and "guess" if they clicked the ad.

I've done this before with pretty darn good results, using this tutorial:

http://www.bennadel.com/blog/1752-Tracking-Google-AdSense-Clicks-With-jQuery-And-ColdFusion.htm

It's not 100% accurate, but it's the best you can do. Basically, when you mouseover the iFrame, it sets a state to watch for loss of focus to your main window. If that happens, it assumes the document lost focus because the user clicked on the iFrame.

Upvotes: 9

Related Questions