Galvanize: Google Analytics without the Javascript

Galvanize is a PHP class for the Google Analytics service that allows you to track page views without the need for Javascript code. Use to to track non-HTML-based content such as podcasts, RSS feeds, downloadable files or anything else that doesn’t render in the browser. You can also use it as a replacement for the regularĀ  Google Analytics scripts on your pages, and not have to rely on Javascript being executed when the page loads.

Usage is simple. Three lines will do it:

include('Galvanize.php');
$GA = new Galvanize('UA-XXXXX-XX');
$GA->trackPageView();

This will register a standard page view for any PHP file on your site. For more flexibility, you can tell it the URL and / or title of the page you want to register.

$GA->trackPageView('testing.php', 'Test Title');

An Alpha release was made available yesterday, which is able to confidently track page views, with event and e-commerce tracking available soon. Download it here or visit the Sourceforge Project Page.

    • Jakob Sternberg
    • October 15th, 2009

    Cool!, been looking for a good non-javascript-version for a while.

    I’ll give it a go! Thanks for sharing!

  1. Great class. Thanks for making life easier for others :)

    • escarp
    • October 20th, 2009

    Code threw the following error when we tried it out:

    Warning: preg_match() [function.preg-match]: No ending delimiter ‘$’ found in /f1/content/escarp/public/Galvanize.php on line 316

  2. Thanks for the bug report. The function matching the referring hostname to the site’s domain and/or the list of ignored referrers wasn’t matching properly.

    I’ve fixed it and uploaded a new version to Sourceforge, but they’re having an issue with file uploads not appearing right away, so watch that space.

  3. Thanks Andy,
    Any luck getting that bug fixed escarp mentioned? I am getting the same issue.

    Thanks!

  4. Hi Mike. Yeah, that bug is fixed in the 0.11 Alpha release. Available on the Sourceforge page now.

  5. This is interesting stuff, though the most important use case for non-javascript tracking is mobile analytics, and I think this project needs to go some way before we can get a lot of value from using it in that way, particularly with devices that do not support cookies, but which we can identify in other ways (eg via end-user login or query string parameters).

    A few suggestions:

    – Don’t rely on cookies to retrieve things like flash version and screen colour depth. Allow them to be set via the class API, so that if the site is identifying mobile handsets from a device database, these vars can be set manually.
    – Provide option to use the server-side session handler to store session-persistent data rather than using cookies (the session handler woudl then normally rely on cookies but the site could implement session tracking in any way it sees fit)
    – The outputJavascript method doesn’t seem to be called anywhere. If that’s intended to allow the script to be a GA one stop shop for both JS supported environments and non-JS environments, then fair enough – though since you are already executing PHP you may as well use it to find out whether you’re serving SSL and then output the correct URL for the ga.js library rather than forcing the user agent to do a document.write of the script tag.
    – You need to expose the ability to set the ‘User defined’ tracking parameter, so that mobile sites can stash stuff like whether the device is touchscreen or supports CSS.

    We’ve done a bit of work in this area but the interpretation of some of the tracking vars has always mystified me to the extent that many of my attempts to track device sessions properly have ended up with no data appearing in GA at all.

    Andrew

  6. Also really important: your retrieveGif method needs a timeout, so that you don’t bring your site down if there’s an issue with GA or your ability to talk to GA. Better still would be to push the hit onto the end of a message queue, and then have a separate process thread asynchronously uploading hits to GA (potentially on a different machine). Doing that would introduce a very slight delay before the hit was reported (I’m thinking less than 5 seconds, typically) but would avoid adding the GA request latency to the time required to render the output to the end user, and would avoid reducing your capacity to serve traffic when under heavy load due to a requirement to make millions of requests to GA on the same platform.

  7. Thanks for the suggestions, Andrew. The outputJavascript() function was intended to output the usual GA javascript but with the additional few lines to store the screen resolution and colour depth in cookies for retrieval later – allowing them to be set by the user is a good idea though, I’ll add it to the development list.

    The user-defined parameter was going to be available on the first Alpha release, but Google changed the way it was handled a couple of days before release. I’m in the middle of re-implementing this – should be there in V0.12 next week.

    I could have sworn I had a timeout in there at one point, but I’ve just checked and it’s definitely not there now. Will add that into V0.12 as well.

    Thanks again for your interest,

    Andy

    • Greg
    • December 11th, 2009

    Hows the work on .12 coming? Any ETA on its release?

    • John
    • January 7th, 2010

    Very nice work! This is a most useful initiative.
    There seams to be a little bug in the latest version though.

    I hope you will be able to release the final version soon..

    Regards,

    John

    • mann
    • January 14th, 2010

    apologies for sounding like a newbie but can this be use for a plain html/css website (i.e. not php) like ours http://www.lotus-architects.com

  8. @Greg – It’s looking like I’ve finally got some free time this weekend so expect something very shortly.

  9. @John – Thanks for the comment. I’ve identified a couple of bugs that need ironing out. I’ll have them sorted very shortly and some additional features to boot.

  10. @mann – You would need to have PHP available on the server, but not necessarily have .php files already. I’m going to be working on simpler ways to add this but if all your content is HTML / CSS you should be fine with a normal Google Analytics installation. Let me know what it was you were trying to track and I’ll see if I can help.

  11. Hi Andy,

    I tried to use it to track a download page with the following code:

    ——————————-
    function download_file() {

    global $file;

    $GA = new Galvanize(UA-XXXXXX-Y);
    $GA-trackPageView(index.php, Download);

    header(Content-Description: File Transfer);
    header(Content-Type: application/octet-stream);
    header(Content-Disposition: attachment; filename=xyz.exe);
    header(Content-Transfer-Encoding: binary);
    header(Expires: 0);
    header(Cache-Control: must-revalidate, post-check=0, pre-check=0);
    header(Pragma: public);
    header(Content-Length: . filesize($file));
    readfile($file);

    }
    ——————————————————–

    and Im getting this error:

    ——————————————————-
    Warning: preg_match() [function.preg-match]: No ending delimiter $ found in /home/curricle/public_html/descarga/Galvanize.php on line 316

    Warning: Cannot modify header information – headers already sent by (output started at /home/curricle/public_html/descarga/Galvanize.php:316) in /home/curricle/public_html/descarga/index.php on line 63

    Warning: Cannot modify header information – headers already sent by (output started at /home/curricle/public_html/descarga/Galvanize.php:316) in /home/curricle/public_html/descarga/index.php on line 64

    Warning: Cannot modify header information – headers already sent by (output started at /home/curricle/public_html/descarga/Galvanize.php:316) in /home/curricle/public_html/descarga/index.php on line 65

    Warning: Cannot modify header information – headers already sent by (output started at /home/curricle/public_html/descarga/Galvanize.php:316) in /home/curricle/public_html/descarga/index.php on line 66

    Warning: Cannot modify header information – headers already sent by (output started at /home/curricle/public_html/descarga/Galvanize.php:316) in /home/curricle/public_html/descarga/index.php on line 67

    Warning: Cannot modify header information – headers already sent by (output started at /home/curricle/public_html/descarga/Galvanize.php:316) in /home/curricle/public_html/descarga/index.php on line 68

    Warning: Cannot modify header information – headers already sent by (output started at /home/curricle/public_html/descarga/Galvanize.php:316) in /home/curricle/public_html/descarga/index.php on line 69

    Warning: Cannot modify header information – headers already sent by (output started at /home/curricle/public_html/descarga/Galvanize.php:316) in /home/curricle/public_html/descarga/index.php on line 70
    ——————————————————–

    Is this a bug or am I doing something wrong?

    Thanks in advance,
    Fernando

  12. Love this class! Great work.

    I setup a small cdn system to make a set of small gif images available to a large forum to use and wanted some way to present a basic url to the image, and still have all the analytic information about page impressions and where the images were being refereed from.

    So through a combination of mod_rewrite rules, some php scripting and your class I can get full analytic information on an image like this:

    http://cdn.indemnity83.com/gif/indemnity83-userbar.gif

  1. No trackbacks yet.