Use Ajax to track Google AdSense ads click

Source: Internet
Author: User
Tags add array object contains key string window access
adsense|ajax|google| Advertising

Function
The data for each user click on the ad can be obtained in full detail. Including the click Time, the user's IP, the page source address, the clicked AD website address, if your site saved the user cookie, you can even find out which user who ordered your ad
With pure client JavaScript code and AJAX technology to achieve click Listening and send to the server click Data, server I use ASP script, just to save click Data and provide browser-side click Query, can be replaced by other server-side script, such as php,jsp, etc.
How to save data in order to be simple, I saved with pure text text, a row to save a piece of data, data fields separated by commas, so that users can save this text in CSV format, CSV will be able to open with Excel.
View Ads Click Data to set access password
Installation and use
Download this file to extract the adlog.asp file inside
Open adlog.asp with Notepad to find the following lines:


Const textfile = "AdLog.txt" Save click on the data text file, is the relative path, you can modify

Const Accesspassword = "Adlog" To view the access password when clicked, you can set it yourself, if you do not need identity authentication, please change the Accesspassword value to ""

Const sessionname = "Google_ad_logger" session name, used to save access password to specified session

Const webcharset = "Utf-8" The type of code for the site, if it is GB-2312, please modify it yourself

Const UserName = "UserName" username cookie, if your station does not have a user cookie, do not care
You can modify its value to suit your actual needs
After the modification, upload the file to your Web site, any path can be. Note here: If your encoding is gb-2312, please open the file in Notepad and choose Save As to change the encoding back to ANSI.
Modify the page file where you put the Google AD and insert the following code anywhere on your page:

Change the page path to your own actual address
After installation, the page will automatically start tracking ads clicked
Click to view the data you can go to ftp directly download your specified text file view, or access to http://www.example.com/adlog.asp
Technical principle
In fact, this is not what advanced technology, principle and simple, the main core functions are in the client JavaScript.

Check to see that Google's advertising code is all in one IFRAME. So first the element set of all the IFRAME tags in the page is saved to an array through document.getElementsByTagName ("iframe")
Then iterate through the array to check whether the IFRAME.SRC value contains a string of "googlesyndication.com", if there is a view that the IFRAME is Google's ad show iframe
Add an onfocus event to the IFRAME after you get the iframe of the Google display ad, which is the event that triggers when the element gets focus
Check the value of the Window.status in the onfocus event, which is the string displayed in the status bar, if you match the "Go" and "key to" string to extract the address of the URL, this address is considered to be clicked on the ad site
Use Ajax to immediately post the user's click Data to the server record, where the Ajax no longer need to respond to the data returned by the server, just send out
Several core codes
A friend who has written a Windows program must know that Microsoft's visual stdio contains a spy gadget, this gadget has a function to listen to a window handle of all the triggering events, it is very good to use, I wrote this ad recorder also implemented a Web page spy , the code is as follows:




<textarea id="view" rows="20" cols="80"></textarea>

This is the way I look at the events triggered on the IFRAME, and after viewing the mouse click IFrame, you can receive the Onbeforeactivate, OnActivate, Onfocusin, onfocus these four, I picked the onfocus to listen to the user clicks.
The Ajax Part I use is the xhcom component, the small nimble is simple, is very good.
In front of the installation I specifically said that contains JS code can be placed anywhere in the Web page, here I use a JS component called Domfunction, this component can implement circular query Dom object, until found the DOM loaded into the page and then execute the code effectively, Equivalent to the role of Document.onload events, Google AdSense ads sometimes show very slowly, using this DOM check program can ensure that every page load can find Google's iframe, foolproof, It also makes it easier for users to add code without considering the impact of the location.
Logically, this program should be written as an ASP plus a JS two files, but read the "single page application" introduced by Brother Andy is very appreciated, so deliberately two files are written together into a file
Server side to save and read the contents of the text file, I used the FSO and ADODB.stream, if your site does not support these components, please contact me, I rewrite other ways
Regret
The most regrettable is that this program can only be used in IE under the start, Firefox can not track the user clicks, because the Firefox ifrmae can not trigger any user action events, to the Mozilla search information, The official explained the use of document.getElementById ("iframe"). Contentwindow this object, but I try to still have no effect, had to drop, waiting for an expert to solve.
The most ashamed of is how I can not find in the onfocus event to determine the user mouse around the key method, in the IFRAME click on the right mouse button will trigger events, the program will be considered an effective user clicks, this problem must be changed.
Demo and Downloads
If you want to see the program actually running immediately, I specifically provided a demo file: http://www.duduwolf.com/test/ adlogdemo.asp, this file does not have access password, anyone can view the click Data, you can also load this string of code immediately from your site to achieve click Tracking, but the click of the data is saved on my website and is open.

Download source files: Google Adsense Click Logger

View Source code: View Source code (UTF-8, if the display is garbled please modify the current page encoding)

Tail
This program I used for several days, the delay has not released is worried about a large number of applications after Google will be modified Google AdSense code, there is I do not know if this is not a violation of the Google Advertising retention terms, carefully reviewed and found that there is such a

Google is strictly prohibited by any means to improve the number of clicks or Web page display times, and through the engineering system and artificial analysis of the behavior of close monitoring.
So I hope you just take it as a technical research, do not long rely on this tracking customer clicks, although the current program does not affect any AdSense launch and click on the action and behavior, but if one day Google staff in person on your site research code is all exposed. The data on my blog has been counted for several days, basic and Google provides similar statistics, because my blog has a user message cookies, so I deliberately saved the user name, want to see who every day is the click of my ads, statistics after I found that the people who clicked on my ads did not leave a message in the blog, The user who keeps a cookie in the blog never clicks on the ad




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.