Skip to content

Instantly share code, notes, and snippets.

@jpalala
Created April 8, 2022 00:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpalala/ba9873a12d2c89ddb3a1dccecbbf0f5b to your computer and use it in GitHub Desktop.
Save jpalala/ba9873a12d2c89ddb3a1dccecbbf0f5b to your computer and use it in GitHub Desktop.
how to log clicks

Source from Log users click without slowing the website down

$(function(){
   $('div.join-link').click(function(){
       var data-url = $(this).attr('data-target-url');
       $.ajax({
           type: 'POST',
           url: 'http://yourdomain.com/log_clicks.php',
           success:function(){window.location.href= data-url;},
           error:function(){window.location.href= data-url;}
       });
   });
})

What the jQuery code does is it initiates an asynchronous http request to http://yourdomain.com/log_clicks.php (the php file that does the actual logging) when any div with the class of join-link. After the codes of log_clicks.php had executed, the success or error function will be run which basically redirects the user to whatever url is specified in the data-target-url attribute of that div.

Now, let's see what the contents of log_clicks.php is:

As you could see in the script, it just basically captures the contents of $_SERVER variable and put it into a MySQL table. The problem with this approach though is, the JQuery code needs to wait for the script to finish before going to the target link. If the MySQL for some reason is slow, or the table where you want to insert the data is being bombarded with read/write operations (specially myisam formats), the execution time of log_clicks.php will be extremely slow. This might not be true if your website has only 100-200 simultaneous users, but when you have thousands of users clicking around your site, this will gonna put a lot of server load in the mysql database and eventually slow the perceived responsiveness of the links in your site.

One solution to this is to make the call to mysql database asynchronous -- that is, the log_clicks.php will return immediately without waiting for the data to be written. That way, the success/error function can run immediately and increase the responsiveness of the site even if the mysql server is slow in accepting the insert statement.

This can be done by writing another script that will sit between the log_clicks.php and mysql database, lets call it log_clicks_async.php

curl_post_async('log_click.php', array());

function curl_post_async($url, $params)
{
    foreach ($params as $key => &$val) {
      if (is_array($val)) $val = implode(',', $val);
        $post_params[] = $key.'='.urlencode($val);
    }
    $post_string = implode('&', $post_params);

    $parts=parse_url($url);

    $fp = fsockopen($parts['host'], 
        isset($parts['port'])?$parts['port']:80, 
        $errno, $errstr, 30);

    $out = "POST ".$parts['path']." HTTP/1.1\r\n";
    $out.= "Host: ".$parts['host']."\r\n";
    $out.= "Content-Type: application/x-www-form-urlencoded\r\n";
    $out.= "Content-Length: ".strlen($post_string)."\r\n";
    $out.= "Connection: Close\r\n\r\n";
    if (isset($post_string)) $out.= $post_string;

    fwrite($fp, $out);
    fclose($fp);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment