Stattraq 1.1.1 and Evan’s fixes

While upgrading to WordPress 2.1 I did a periodic spin through my plugins to get the latest and greatest updates. I noticed that Stattraq 1.0b was no longer the latest update — in fact, there’s a new maintainer: “Murph” and he’s released a version 1.1.1 with a number of fixes.

Unfortunately, I had made a bunch of tweaks to 1.0b over the year I was running it. Bug fixes, performance improvements, anti-referrer-spam stuff. Mostly stuff I hacked together myself or picked up from other blog comments while I was troubleshooting some problem.

The good news is that windiff showed me that a number of these fixes were included in 1.1.1. The bad news is that windiff showed me that a number of these fixes were NOT included in 1.1.1

So I made the fixes again to my copy of stattraq.php. And then I tried to post the details back to Murph’s blog so that they could hopefully get integrated going forward. But the blog comment posting stripped all the meta data out and undoubtedly makes the changes hard to follow.

So, here (below) is what my blog post was supposed to look like. Also, here’s a link to my modified stattraq.php file in case you want to use it and/or DIFF it with your own changes.

The intended comment:

Couple of things I had fixed in my copy of 1.0b that are “rebroken” in the 1.1.1 release. Please consider integrating these so I can just run the baseline going forward:

1) Blogs using pretty permalinks (ie – not “?p=64” style) don’t properly break page views out into the list of pages; everything shows up as “multiple pages”. This is because it’s not able to resolve the page view back to the actual page.

Changes:
$s_id = session_id();
<! // **EVAN – Move $urlRequested up to here (need it before you can get the article ID) and fix it to be the right value
<! $urlRequested = $_SERVER[‘PHP_SELF’] . $_SERVER[‘REQUEST_URI’] ;
<! // ** EVAN – End of $urlRequested change (except for commenting the original out below)
<!
// need to get the real article_id or type of server request (RSS, RDF, ATOM, Ping, etc)

and

$ipAddress = statTraqGetIPAddress();
<! // $urlRequested = $_SERVER[‘PHP_SELF’] . (isset($_SERVER[‘QUERY_STRING’]) ? “?”.$_SERVER[‘QUERY_STRING’] : ” );
!> $urlRequested = $_SERVER[‘PHP_SELF’] . (isset($_SERVER[‘QUERY_STRING’]) ? “?”.$_SERVER[‘QUERY_STRING’] : ” );
$browser = statTraqGetBrowser();

2) Make sure favicon.ico is excluded from tracking also. Also, change the insert style to “DELAYED” to improve performance.

Changes:
}
<! // **EVAN – also exclude favicon.ico
<! if (strstr($urlRequested, “favicon.ico”))
<! $isIgnored = true;
<! // **EVAN – end favicon.ico change
<!
if (!strstr($_SERVER[‘PHP_SELF’], ‘wp-admin’) && !strstr($_SERVER[‘PHP_SELF’], ‘wp-stattraq’) && !$isIgnored)
<! // **EVAN – make the “insert” a “Delayed” insert for performance
<! $wpdb->query(“INSERT DELAYED INTO $tablestattraq (session_id, access_time, ip_address, url, article_id, referrer, user_agent, browser, user_agent_type, search_phrase) values (‘”.$s_id.”‘, NOW(), ‘$ipAddress’,’$urlRequested’, ‘$article_id’, $referrer,’$userAgent’,’$browser’, $browser_type, ” . ($search_phrase==null?”NULL” : “‘$search_phrase'”) . “)”);
!> $wpdb->query(“INSERT INTO $tablestattraq (session_id, access_time, ip_address, url, article_id, referrer, user_agent, browser, user_agent_type, search_phrase) values (‘”.$s_id.”‘, NOW(), ‘$ipAddress’,’$urlRequested’, ‘$article_id’, $referrer,’$userAgent’,’$browser’, $browser_type, ” . ($search_phrase==null?”NULL” : “‘$search_phrase'”) . “)”);
$wpdb->show_errors();

3) Added in some additional referrer parsing plus antispam protection against some spam referrer strings

Changes:
$key = “q”
<! // **EVAN – added a few more search engines to parse for
<! }else if(strpos($referrer, “icerocket.”)!== false || strpos($referrer, “search.blogger”) !== false){
<! $key = “q”
<! }else if(strpos($referrer, “blogsearchengine.”)!== false){
<! $key = “p”
}else if(strpos($referrer, “yahoo.”)!== false){
$key = “p”
}else if(strpos($referrer, “aol.”) !== false || strpos($referrer, “netscape.”) !== false){
$key = “query”
}
<! // **EVAN – Antispam for search queries
<! else if(
<! strpos($referrer, “bingo”) !== false ||
<! strpos($referrer, “backgammon”) !== false ||
<! strpos($referrer, “casino”) !== false ||
<! strpos($referrer, “oyun”) !== false
<! )
<! {
<! return null;
<! }
<! // ** EVAN – end Antispam for search queries
if($s != null && $s != ”){return $s;}

8 Comments

  1. YeOleImposter said,

    February 21, 2007 at 8:27 pm

    Thanks! Looks like these are some of the same changes I made to my 1.0b — I was suprised that they were not incorporated into 1.1.1.

    I appreciate you posting your copy of the .php file.

  2. StatTraq for WordPress 2.xat the Dogberry Patch said,

    February 21, 2007 at 11:57 pm

    […] incorporated into Murph’s version, but not all. I was very glad to find that another user, Evan, had already gone to the trouble of patching Murph’s version to include these […]

  3. Evren said,

    March 19, 2007 at 1:18 am

    Thanks for the fix, it totally worked for me also.
    If was thinking go back to 1.0b before finding your fix.

  4. Gadget User Blog » Blog Archive » Web stats updated said,

    April 7, 2007 at 6:36 pm

    […] So both solutions are very close and it comes down to a matter of personal preference. There is one trick with StatTraq that you’ll need to implement if you are using the long version of permalinks (e.g., domain/category/post_title as opposed to domain/?=p34), which is not well explained on the plugin’s web site but happily is very well covered by Evan Dodds on this site. […]

  5. Vincent Sparreboom said,

    October 14, 2007 at 5:18 am

    Hi,

    I was wondering if you could help me out I also posted this on the stattraq page.

    I got this error message…
    Warning: Unknown: open(/tmp/sess_a7501ec91ec3a3c590fecbc4701e595e, O_RDWR) failed: Permission denied (13) in Unknown on line 0

    Warning: Unknown: Failed to write session data (files). Please verify that the current setting of session.save_path is correct () in Unknown on line 0

    I just upgraded to WP2.3 and at first it seems to go well. Now I upgraded to v1.1.1 and even tried your fix but all without any luck. The blog itself is running fine except for this error message.

    Do you have any advice on how to handle this, I love this plugin.

    Regards

    Vincent

  6. Evan said,

    October 14, 2007 at 7:30 am

    Hi Vincent – unfortunately, I’ve quit using Stattraq as part of my WP2.3 upgrade, so I can’t tell you whether it’s something particular to your environment or something about Stattraq with WP2.3. When do you get this error message? Is Stattraq actually working fine or is it not working?

  7. Vincent Sparreboom said,

    October 16, 2007 at 3:58 am

    Hi Evan, thanks for your reply. It was working and my blog was also running fine. I only got this error message.

  8. Aaron said,

    April 30, 2008 at 12:36 pm

    I was pulling my hair out trying to get the permalink issue resolved but your updated file worked beautifully. Thank you for sharing this!