Forum Moderators: coopster

Message Too Old, No Replies

PHP page view counter 1/10th the visitors Goog Analytics shows

         

internetheaven

8:32 pm on Mar 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have this code in the php script that generates the user side pages:

if (!empty($_COOKIE['__utma']))
{
$query = "SELECT views FROM `article` WHERE
`articletag`='{$_GET['show']}'";
$views['art'] = $db->query($query);
$views = $views['art']['0']['views'];
if (empty($views)){$views = '0';}
$views++;
$query = "UPDATE `article` SET `views` = '{$views}' WHERE
`articletag`='{$_GET['show']}'";
$db->query($query);
}

It is supposed to count a view (updated in the number in "views" for that article) every time the entry is called but exclude views by crawlers.

Today Google Analytics shows 235 views for one page but only 18 have been logged into the database by the script. I believe Google Analytics.

Does anyone know what might be wrong with this code? The script has to run for the page to display and the script will run before the Google Analytics code even gets called into the templated page.

Any help would be really appreciated!
Thanks
Mike

eeek

11:07 pm on Mar 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



What happens when several views happen more or less at the same time?

but exclude views by crawlers.

That might be the source of the problem.

eeek

11:08 pm on Mar 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



if (empty($views)){$views = '0';}

So if the select fails for any reason, you reset the count to zero?

internetheaven

7:08 am on Mar 24, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



but exclude views by crawlers.

That might be the source of the problem.

How could that be the source of the problem? Google analytics doesn't count crawlers either.

omoutop

9:46 am on Mar 24, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If you believe google is correct, then the problem lies in your crawler detection script.

idfer

4:51 pm on Mar 24, 2009 (gmt 0)

10+ Year Member



As eeek mentioned, you might have problems with concurrency. It's better to make your update statement atomic. Make sure you have a unique index on the articletag field, then change the update statement to this:

INSERT INTO views VALUES ($articleTag, 1) ON DUPLICATE KEY UPDATE views = views +1;

And the usual caveats apply to using $_GET['show']: make sure you escape special chars and its length doesn't exceed the field size.

It's also possible that $_GET['show'] doesn't contain the value you're expecting. Check the table's contents to see if there are any strange rows in there, also check the return value from $db->query() and log an error if it's wrong.

internetheaven

5:53 pm on Mar 24, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



INSERT INTO views VALUES ($articleTag, 1) ON DUPLICATE KEY UPDATE views = views +1;

Thanks, I'll look into that one.

To clarify, the URl is:

www.example.com/folder/file.php?show=1111111

the number corresponds with a unique article in the database.

File.php contains the code I listed above.

Thanks
Mike