I’m watching you

I do enjoy Redhat’s Openshift service. It’s fast and easy to get up and running. Using this tool I’m able to run my own version of Analytics. Why would I do this when Google Analytics is a perfectly cromulent choice? Well, sometimes it’s not. Case in point, Blogger.com. I have been posting on my writers group blog on a weekly basis and found myself jonesing for traffic data. Blogger is owned by Google and Google loves analytics, so I thought I found a happy place. Turns out no. Yes, they tell me the basics, who and from where, but for me the prime metric to watch is Bounce Rate (BR). That magical percentage that shows how many visitors you have come, then go. A large number indicates your keyword or title is misleading, and leads to mis-trust in future posts. I would like a number around 40%. Realistically, for a blog post, it’ll probably be around 60%, which is frighteningly high for normal landing pages.

JavaScript and some PHP to the rescue!

For the blog post I slide into each a snippet of javaScript

 *  Call the jQuery libaray
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>

 *  Setup interval to fire off every second for the first 2 minutes.
 *  Send data to my server for collection.
<script type="text/javascript">
setInterval(function() {
    //$("p").append(+new Date);
    var  created = +new Date
    $.post("http://My_SERVER.com/api.php", { visitor: myIP(), site: "blog post title" });
}, 1000, 120);

 *  Grab the visitors IP address
function myIP() {
    if (window.XMLHttpRequest) xmlhttp = new XMLHttpRequest();
    else xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      xmlhttp.open("GET"," http://api.hostip.info/get_html.php ",false);
      hostipInfo = xmlhttp.responseText.split("\n");
       for (i=0; hostipInfo.length >= i; i++) {
         ipAddress = hostipInfo[i].split(":");
         if ( ipAddress[0] == "IP" ) return ipAddress[1];
     return false;

When I was first trying out this script I didn’t add the 120 second timeout. Bad idea. Someone in San Jose opened the page and for two days had it open. That was a doh! moment for me and I realized anything past 2 minutes didn’t matter. It wasn’t a bounce!

For the server side I opted to use Openshift’s PHP 5.4 cartridge and MySQL. There are two parts to the server, the POST API and the means to present the data. To receive the post from the javascript I have a small script waiting to catch and save the data, and named it api.php:


 * MySQL Credentials
define('DB_HOST', getenv('OPENSHIFT_MYSQL_DB_HOST'));
define('DB_PORT', getenv('OPENSHIFT_MYSQL_DB_PORT'));
define('DB_NAME', getenv('OPENSHIFT_GEAR_NAME'));

 * MySQL Connection
$dsn = 'mysql:dbname='.DB_NAME.';host='.DB_HOST.';port='.DB_PORT;
$dbh = new PDO($dsn, DB_USER, DB_PASS);
 * Look for the POST data and
 * assign to variables
if ( isset($_POST) ) {
 foreach ( $_POST as $key => $value ) {
 if ( $key == 'site') {
 $site = $value;
 if ( $key == 'visitor') {
 $visitor = $value;

 * Populate table with visitor, timestamp and blog post title
 $now = time();
 $sql = $dbh->prepare('INSERT INTO log (line_created, date_created, tracking) VALUES (:line_created, :now, :tracking)');
 $sql->execute(array(':line_created' => $visitor, ':now' => $now, ':tracking' => $site));



Fairly straightforward capturing of data into a table I call log:


| Field        | Type         | Null | Key | Default | Extra |


| line_created | varchar(255) | YES  |     | NULL    |       |

| date_created | varchar(255) | YES  |     | NULL    |       |

| tracking     | varchar(255) | YES  |     | NULL    |       |


As this is all just proof of concept I do not have audit tracking or security measures in place…yet.

Next post: Displaying all this data


If you found this helpful, a couple of mBTC’s are always welcome 🙂



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s