Ashish
Ashish

Reputation: 303

Difference in no of visits in Google analytics reports and page counter

Hi i have a website Link in which i have inserted Google analytics code for daily reports. I also have inserted an Update query on each of my web page that is updated by 1 each time a page is visited.

Query

$date= date("Y-m-d");
$count=1;

if ($stmt = $mysqli->prepare("SELECT *,count(*) as cnt FROM Table WHERE date=?")) {
    $stmt->bind_param('s',$date);
    $stmt->execute();
    $res = $stmt->get_result();             
    $row = mysqli_fetch_array($res);
    $cnt= $row['cnt'];
    if($row['cnt']==1){
        $count=$row['count']+1;
        $stmt = $mysqli->prepare("UPDATE Table SET counts='$count' WHERE date=?");
        $stmt->bind_param('s',$date);
        $stmt->execute();
    }else{
        $stmt = $mysqli->prepare("INSERT INTO Table VALUES('',?,?)");
        $stmt->bind_param('ss',$date,$count);
        $stmt->execute();
    }
}

Google Analytics code

<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-103015467-1', 'auto');
  ga('send', 'pageview');

</script>

Now when i check both at morning, It shows a huge difference in daily visits. Reports of last 4 days are as

Date        G.Ana   UpdtQuery

2-Aug       70      489
3-Aug       59      581
4-Aug       46      1240
5-Aug       71      611
6-Aug       37      508
7-Aug       58      1189

Why there is a lot of difference in Google Analytics reports and visits counted by update query ?? Thanks in Advance...

Upvotes: 1

Views: 71

Answers (2)

Dmitry Sulman
Dmitry Sulman

Reputation: 126

The main difference between your data and Google Analytics data is that your code works server side but Google Analytics tracking code works client side. It means your code will count every time your page will be requested, including many requests from different bots. GA instead will only count hits from clients with JavaScript support.

Upvotes: 1

Dmitry Sulman
Dmitry Sulman

Reputation: 126

Your code counts hits (or pageviews). What type of Google Analytics metric do you look at when you make your comparison? If you look at sessions it is quite different metric. One user session can contains many pageviews that's why values of GA session metric exceeds values from your DB.

Upvotes: 0

Related Questions