Search code examples
phpgoogle-apihitcounter

mismatched counter value, Google shortener vs php visitor counter


This is the flow of what I am trying to achieve:

user

  1. Click shared link (hits Google shortener link)
  2. Redirect to counter (counter.php)
  3. Redirect to the web page (the website page)

I have a problem with the counter value from the Google shortener and my own counter - the value always different. My counter value is always bigger than Google shorter value, is there anything wrong with my own code counter?

counter.php :

//redirect to ?
$destination = "page1";
$destination_mobile = "page2";
$destination_article = "page3";

//connection
$con = mysqli_connect('localhost','root','','db_omron');
if (!$con) {    
    die('Could not connect: ' . mysqli_error($con));
}

//source contents
$id = mysqli_real_escape_string($con, $_GET['id']);
//internal
if($id=='101010'){$source='Quiz';}
else if($id=='101011'){$source='Quiz mobile';}
else if($id=='101020'){$source='Event';}
else if($id=='101021'){$source='Event mobile';}
else if($id=='101031'){$source='SMS';}
else if($id=='101040'){$source='News';}
else if($id=='101041'){$source='News mobile';}
else if($id=='101050'){$source='Banner';}
else if($id=='999990'){$source='tester';}
else{$source='n/a';}

date_default_timezone_set('Asia/Jakarta');
$today = date("Y-m-d");

//1.get old data
$sql="SELECT * FROM `click_counter` WHERE `id_source` = $id ORDER BY `date` DESC";
$result = mysqli_query($con,$sql);

$row = mysqli_fetch_array($result);
$oldTotal=$row['total'];
$oldDate=$row['date'];

//2.check if today is in database
if($today==$oldDate){
    //update
        $newTotal = $oldTotal+1;
        $newSqlUpdate="UPDATE `click_counter` SET `total` = '$newTotal' WHERE `id_source` = $id AND `click_counter`.`date` = '$today'";
        $newResult = mysqli_query($con,$newSqlUpdate);
    }else{
    //insert
        $newSqlInsert="INSERT INTO `click_counter` (`date`, `id_source`, `source`, `total`) VALUES ('$today', '$id', '$source', '1')";  
        $newEntry = mysqli_query($con,$newSqlInsert);
    }

//close connection
mysqli_close($con);

//redirecting page 

$a = substr("$id" , -1);
if ( $a == "1" ){
    header("Location:".$destination_mobile); 
}else if(($id=='102120') || ($id=='102130')){
    header("Location:".$destination_article); 
}else{
    header("Location:".$destination);
}

exit();

Solution

  • I have a problem with the counter value from the Google shortener and my own counter - the value always different. My counter value is always bigger than Google shorter value, is there anything wrong with my own code counter?

    Google Analytics tracks via JavaScript, and as such, most bots and any users without JS enabled will not be tracked in their numbers. This is almost certainly the cause of your discrepancy.