How to Track AdSense Clicks

Discussion in 'Reporting & Stats' started by clancey, Oct 6, 2006.

  1. #1
    I keep coming across the question of how to track AdSense clicks. I found a great solution at http://www.tech-pro.net/track-adsense-clicks.html

    They provide a piece of javascript which can be inserted into each page. It allows for the creation of an extended entry in your web servers log, which contains the URL of the ad that was clicked. It calls an empty PHP script putting information about the click in the query to the script.

    I modified the javascript slightly so that the logging script could be located on a third party server. Instead of an empty PHP script, mine logs the date, time stamp, visitors IP address, and the query to a TABBED, plain text file. My solution is located at http://www.10-minute-rule.com/index.php?itemid=123

    The Javascript is:

    
    <script type="text/javascript"><!--
    function log()
    {
    if (window.status.indexOf('go to') == 0)
    {
    img = new Image();
    img.src = 'http://www.MYDOMAIN.com/analysis.php?Ref=' + document.location 
    + '&Url=' + window.status.substring(6) 
    + '&Size=' + event.srcElement.width 
    + 'x' + event.srcElement.height;
    }
    }
    
    var elements;
    elements = document.getElementsByTagName("iframe");
    for (var i = 0; i < elements.length; i++)
    {
    if(elements[i].src.indexOf('googlesyndication.com') > -1)
    {
    elements[i].attachEvent("onfocus",log);
    }
    }
    //--></script>
    
    Code (markup):
    Change MYDOMAIN.com to your domain.

    And the PHP script is:

    
    <?php
    $query = strtolower( $_SERVER["QUERY_STRING"] );
    LogEntry( "/PATHTO/MYHOME_DIRECTORY/ad-clicks", $query);
    ?>
    
    <?php
    ######################################################
    #
    # Logging
    
    function LogEntry( $logfile, $message)
    {
    $date = date('M-d-Y');
    $timestamp = time();
    
    $message = $date . "\t" . 
    $timestamp . "\t" . 
    get_client_ip() . "\t" . 
    $message . "\n";
    
    $file = fopen( $logfile, "a");
    fwrite( $file, $message );
    fclose($file);
    }
    
    
    ##############################
    # retrieve the IP address of the user
    function get_client_ip() {
    if (isset ($_SERVER['HTTP_X_FORWARDED_FOR']))
    $ipAddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
    else
    $ipAddress = $_SERVER['REMOTE_ADDR'];
    return $ipAddress;
    }
    
    ?>
    
    Code (markup):
    Change "/PATHTO/MYHOME_DIRECTORY/ad-clicks" to a path and filename which works on your server and which works for you.
     
    clancey, Oct 6, 2006 IP
  2. khan11

    khan11 Active Member

    Messages:
    615
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    58
    #2
    could you please guide me if i can put the code in css file so it will be easy.
     
    khan11, Oct 6, 2006 IP
  3. clancey

    clancey Peon

    Messages:
    1,099
    Likes Received:
    63
    Best Answers:
    0
    Trophy Points:
    0
    #3
    The javascript code needs to go before the closing body tag on your page. I use CSS to define the attributes of tags, not the content of the page.

    On my site pages are comprised of three sections -- header file, content file, and footer file. I placed the javascript in the footer file just before the closing </body> tag.
     
    clancey, Oct 6, 2006 IP
  4. Scriptona

    Scriptona Notable Member

    Messages:
    4,957
    Likes Received:
    265
    Best Answers:
    0
    Trophy Points:
    280
    #4
    I simply use Adlogger script from Adlogger.org

    it's FREE and tracks ad page/ip of click blocks ips that do click fraud

    i love it so far
     
    Scriptona, Oct 6, 2006 IP