Use AJAX to track Google Adsense ad clicks

Source: Internet
Author: User

Function

  1. You can obtain the data of each user clicking an advertisement in full detail. IncludingClick Time,User IP Address,Page Source Address,The address of the clicked advertisement website.If your site stores user cookies, you can even queryWhich user ordered your advertisement?
  2. The client-only javascript code and AJAX technology are used to implement click listening and send click data to the server. The server uses ASP scripts to store click data and provide browser-side click queries, it can be changed to other server scripts, such as PHP and JSP.
  3. To save data in a simple way, I use plain text to save one line of data and separate data fields with commas (,), so that you can save the text as csv format, csv can be opened in excel.
  4. View ad click Data Set access password
Installation and Use
  1. Download this file to decompress the adLog. asp file.
  2. Open adLog. asp in notepad and find the following lines:
    Const TextFile = "adLog.txt" ': The text file that saves the clicked data. It is a relative path and can be modified.
    Const AccessPassword = "adLog" 'You can set the access password when you click it. If you do not need identity authentication, change the value of AccessPassword "".
    Const SessionName = "google_ad_logger" 'session name, used to save the access password to the specified Session
    Const WebCharset = "UTF-8" 'specifies the encoding type of the website. If it is a GB-2312, modify it by yourself.
    Const UserName = "username" 'user name cookie. If your website does not have a user cookie, you can modify the cookie value based on your actual needs.
  3. After the modification is completed, upload the file to your web site, which can be any path. Note:If your encoding is gb-2312, open the file in notepad and choose Save as to change the encoding back to ANSI.
  4. Modify the page file of your google advertisement and insert the following code into any location of your page: Modify the page path to your actual address.
  5. After the installation is complete, the page will automatically start to track ads and click
  6. View click data you can directly download your specified text file on ftp to view, or access http://www.example.com/adlog.asp
Technical Principles

In fact, this is not an advanced technology, the principle and simplicity, the main core functions are in the client javascript.

  • You can see that all google's ad code is placed in an iframe. Therefore, first use document. getElementsByTagName ("iframe") to obtain the element set of all iframe labels on the page and save it to an array.
  • Then, traverse the array and check whether the iframe. src value contains the string "googlesyndication.com". If so, the iframe is regarded as the iframe displayed by google ads.
  • After obtaining the iframe of the advertisement displayed by google, add an onfocus event to the iframe. This event is triggered when the element obtains the focus.
  • In the onfocus event, check window. status value, that is, the string displayed in the status bar. if you match the strings "go to" and "connect to", the URL is extracted, this address is considered as the clicked advertisement URL.
  • Use AJAX to immediately POST the user's click data to the server for record. Here, AJAX no longer needs to respond to the data sent by the server, just send it out.
Several core codes
  • A friend who has compiled a windows program must know that Microsoft's Visual Stdio contains a Spy tool, which provides a function to listen to all trigger events of a window handle, it's easy to use. When I write this advertisement generator, I also implement a Spy on a webpage. The Code is as follows:


    I used this method to view the events triggered on iframe. After checking, I found that the events received when I clicked iframe contain onbeforeactivate, onactivate, onfocusin, and onfocus, I chose onfocus to listen to users' clicks.
  • I use XHCom components for ajax, which are small, flexible, and simple.
  • During the installation, I specifically mentioned that JavaScript code can be stored anywhere on the webpage. Here I use a js component called domFunction, which can be used to query DOM objects cyclically, it is equivalent to document. the role of the onload event, google adsense ads sometimes show very slow, with this Dom check program, you can ensure that google iframe can be found every page load, foolproof, it also makes it easier for users to add code without considering the impact of location.
  • By default, this program should be written as an asp file and a js file. However, I really appreciate the article "single page application" introduced by Andy, therefore, the two files are specially written into one file.
  • When the server saves and reads the content of a text file, I use FSO and ADODB. Stream respectively. If your site does not support these components, please contact me and I will rewrite other methods
Sorry
  • The most unfortunate thing is that this program can only be enabled under IE, and users cannot be tracked for click operations in Firefox, because ifrmae in Firefox cannot trigger any user operation events at all and goes to Mozilla to check information, document. getElementById ("iframe "). contentWindow object, but after I try it, it still does not work, so I have to stop it and wait for the superior to solve it.
  • The most embarrassing thing is that I cannot find a way to judge the left and right keys of the user in the onfocus event. right-clicking the iframe will trigger the event, the program will be considered a valid user click. This problem must be changed.
Demo and download

If you want to immediately check the actual running of the program, I specifically provide a demo file: Running.
Download source file: Google Adsense Click Logger
View Source code online: view the source code (UTF-8. If garbled characters are displayed, modify the encoding on the current page)

Tail

I have used this program for several days. If it is not released, I am worried that google will modify the google adsense code after a large number of applications, I don't know if this is against the retained terms of google ads.

Google prohibits the use of any means to increase the number of clicks or display pages, and closely monitor behavior through engineering systems and manual analysis.

Therefore, we hope that you will only use it for technical research and do not rely on this tracking client to click for a long time. Although this program does not affect any adsense serving and clicking actions and actions, however, if one day google's staff went to your site to study the code, it would be completely exposed. The data on my blog has also been counted for several days, which is basically similar to the statistics provided by google. Because my blog contains cookies for users' messages, I specially saved the user name, I want to see who is clicking my advertisement every day. After statistics, I found that no one clicking my advertisement left a message in the blog, and the user who saves the cookie in the blog never clicked on the advertisement.


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.