Google Analytics on RedBubble

I’ve been using RedBubble since October of this year to sell some of my photographs. It’s a great site, with a thriving, fun artist community. The site is still relatively young, and is missing a few features that would be very helpful to those trying to sell their works. One such feature is a tracking system, like Google Analytics, which would help artists better target communicaitions and advertising campaigns. At this point RedBubble does not offer Google Analytics support, but there is a way to pass limited information along to Google Analytics for tracking purposes.

Google Analytics (GA) works by doing two things. Passing information from a cookie to GA and by downloading a small image file from GA.

Because GA uses Javascript you can’t simply add the code to your RedBubble site. However, you can link to an image file on another server, and that opens the door to using a file on another server to pass information through to GA via the image download function.

I found this post that explains how it’s done. The post also provides php code. I’ve created this brief walk-through for RedBubblers.

Here is what you need to use this method:

  1. A server where you can add a php file, a directory, and image files at will.
  2. The ability to redirect on that server
  3. A new GA urchin number. (You don’t want to corrupt the statistics of another site)

Here’s the idea.

Joe Visitor loads up your redbubble page. On that page there is a link to an image file: !http://www.yoursite.com/tracker/main.gif! (This is the code RedBuble uses for inserting images into text.) The tracker directory doesn’t exist, instead there is a command to redirect that image file to a php file called track.php. Track.php passes along the file that’s being loaded to GA and gives Joe the new image url, which is http://www.yoursite.com/imagedir/main.gif

Okay, here’s the technical stuff:

In .htaccess you need to add the following line:

* RewriteRule ^tracker/(.*).gif$ /track.php?url=$1&filetype=gif

This is what causes Joe’s browser to load track.php instead of /tracker/main.gif

Now you need to edit your php file. Copy the following code into a new text file.

<?php
$var_utmac=’UA-0000000-0′; //enter the new urchin code
$var_utmhn=’www.redbubble.com/people/yourname’; //enter your domain
$var_utmn=rand(1000000000,9999999999); //random request number
$var_cookie=rand(10000000,99999999); //random cookie number
$var_random=rand(1000000000,2147483647); //number under 2147483647
$var_today=time(); //today
$var_referer=$_SERVER[‘HTTP_REFERER’]; //referer url

$var_uservar=’-‘; //enter your own user defined variable
$var_utmp=’tracker/’.$_GET[‘url’].’.’.$_GET[‘filetype’]; //this example adds a fake file request to the (fake) tracker directory (the image/pdf filename).

$urchinUrl=’http://www.google-analytics.com/__utm.gif?utmwv=1&utmn=’.$var_utmn.’&utmsr=-&utmsc=-&utmul=-&utmje=0&utmfl=-&utmdt=-&utmhn=’.$var_utmhn.’&utmr=’.$var_referer.’&utmp=’.$var_utmp.’&utmac=’.$var_utmac.’&utmcc=__utma%3D’.$var_cookie.’.’.$var_random.’.’.$var_today.’.’.$var_today.’.’.$var_today.’.2%3B%2B__utmb%3D’.$var_cookie.’%3B%2B__utmc%3D’.$var_cookie.’%3B%2B__utmz%3D’.$var_cookie.’.’.$var_today.’.2.2.utmccn%3D(direct)%7Cutmcsr%3D(direct)%7Cutmcmd%3D(none)%3B%2B__utmv%3D’.$var_cookie.’.’.$var_uservar.’%3B’;

$handle = fopen ($urchinUrl, “r”);
$test = fgets($handle);
fclose($handle);

switch ($_GET[‘filetype’]){
case ‘jpg’:
header(‘Content-Type: image/jpeg’);
break;
case ‘gif’:
header(‘Content-type: image/gif’);
break;
case ‘pdf’:
header(‘Content-type: application/pdf’);
break;
// add your own content types where needed
}

$imageurl = fopen (‘http://www.yoursite.com/directory/’.$_GET[‘url’].’.’.$_GET[‘filetype’], “r”); //this is where the real file should be located
while (!feof ($imageurl)) {
$image = fgets($imageurl, 4096);
echo $image;
}
fclose($imageurl);
?>

There are three lines you MUST update for this to work:

  1. Line 2: $var_utmac=’UA-0000000-0’; (Enter your urchin code)
  2. Line 3: $var_utmhn=’www.redbubble.com/people/yourname’; (This needs to be the same address you used when you signed up for GA)
  3. Line 32: $imageurl = fopen (‘http://www.yoursite.com/imagedir/’.$_G…… (Change the address to where the images are actually located)

After you’ve edited it, rename it to track.php and upload it to the root directory of your Web server.

Testing!

Add a gif file to http://www.yoursite.com/imagedir/ called test.gif (remember, the redirect is setup to work with gif files).

Then go to http://www.yoursite.com/tracker/test.gif you should be redirected to http://www.yoursite.com/track.php?url=test&filetype=gif and you should see the test.gif image shown. If the image is shown everything is working! If you get php errors then there is something wrong with your php file. If you get a 404 error your redirect isn’t working.

Test two: Add !http://www.yoursite.com/tracker/test.gif! to your RedBubble profile page. Reload your profile page to see if test.gif appears.

If you pass both these tests then you know your redirect and php file are working.

Now, you’ll want to add a transparent 1×1 gif to your directory so that you can load an image that isn’t intrusive to your profile.

Add a link to that clear image to every page you want tracked.

You should notice redirections from redbubble.com immediately in your redirect log file. However, GA information won’t show up for at least a few hours. (The date defaults to yesterday in GA, so to check that you are getting results set the date to today.)

It’s important to note that using this method will only tell you which image files on yoursite.com are looked at. Every single visit will come from the server where yoursite.com is hosted. You won’t get any referral information, visitor location information, etc.

If you use the same image file for every page you’ll simply get a list of how many times that file has been viewed. If you create a different image file for each page you want tracked you can see how many times each page is viewed. You will also see what days and times of day the files are viewed.

I’m looking into ways of getting more information passed from Joe to GA to make this more useful. If anyone has any ideas or suggestions please let me know!