Ajax link tracker

Posted By: Ian on Feb 15, 2014 in JQuery
Last modified on January 29th, 2015 at 9:31 pm,

Q. How can i record all the external links clicked on my site so i can see where they are when they click on the link, where they came from before and where there going?

A. Add the script below to your pages and the information will be sent too count.php for you to manipulate as you wish.

<script type="text/javascript">
/**
 * Wrap everything in a Self-Executing Anonymous Function
 */
$(function () {
    /**
     * Assign click handler to all a tags
     */
    $('a').click(function () {
        /**
         * If host of the a tag clicked on is not the same as host were on and it does
         * not contain the class stop carry on
         */
        if ((this.host !== location.host) && (!$(this).hasClass("stop"))) {
            /**
             * Add key value pairs in object and declare it as a variable
             */
            var where = {
                'From': document.referrer,
                'Too': window.location.href,
                'Left': this.href
            };
            /**
             * Post data as string with variable so we can pick it up at other end
             */
            $.post('count.php', {
                result: JSON.stringify(where)
            })
        }
    })
});
</script>

As you can see by the code above the script only sends the information to count.php if the host is not the same as the host it’s on and that the classname stop is not found.

The below is a demo to show it working but ideally you probably want to store the data in a database or something but I’ll leave that up to you

<?php
if (isset($_POST['result'])) {
    /**
     * Declare variable for posted data $_POST['result']
     */
    $data   = $_POST["result"];
    /**
     * Return posted data as associative array
     */
    $data   = json_decode("$data", true);
    /**
     * Declare variables for accessing values from keys in array
     */
    $from   = $data['From'];
    $too    = $data['Too'];
    $left   = $data['Left'];
    /**
     * Just for a demo
     */
    $myFile = "visitor.txt";
    $fh = fopen($myFile, 'w') or die("can't open file");
    $stringData = "Visitor came from " . $from . "\nThen went too " . $too . "\nBefore leaving for " . $left;
    fwrite($fh, $stringData);
    fclose($fh);
}
?>

Which in this example writes to a text file called visitor.txt

Visitor came from http://ianjgough.com/projects/spot-the-child-at-risk-bullying/
Then went too http://ianjgough.com/php/banner-bar-125/
Before leaving for http://http://graphicriver.net/?ref=ianjgough
AnonymousIan.J.Gough

Did this help you? Please Let me know by commenting below even if it’s just to say “Thanks”

leave a comment

About This Site

Dreams are built from lines of code well it's true!
If you can dream it you can usually code it.

I have learned so much from other people on the Internet and this site is for me to give back some of what i have learned and hope people can now learn from me.
Have fun and surf safely,
Ian.J.Gough

protected by copyscape duplicate content check

Categories


Powered by Banner Bar 125