views:

22

answers:

1

Basically I want to run the Google Analytics on the server and not show the 1px .GIF (the reason for this is that I want to display an image without it being in HTML but as a Content-type: image/jpeg

    $GA_ACCOUNT = "MO-3845491-5";
      $GA_PIXEL = "ga.php";

      function googleAnalyticsGetImageUrl() {
        global $GA_ACCOUNT, $GA_PIXEL;
        $url = "";
        $url .= $GA_PIXEL . "?";
        $url .= "utmac=" . $GA_ACCOUNT;
        $url .= "&utmn=" . rand(0, 0x7fffffff);

        $referer = $_SERVER["HTTP_REFERER"];
        $query = $_SERVER["QUERY_STRING"];
        $path = $_SERVER["REQUEST_URI"];

        if (empty($referer)) {
          $referer = "-";
        }
        $url .= "&utmr=" . urlencode($referer);

        if (!empty($path)) {
          $url .= "&utmp=" . urlencode($path);
        }

        $url .= "&guid=ON";

        return $url;
      }

$googleAnalyticsImageUrl = googleAnalyticsGetImageUrl();
  //echo '<img src="' . $googleAnalyticsImageUrl . '" />';
  //open the google script with all our settings to generate the 1px x 1px blank gif which reports to GA
  $handle = fopen ($googleAnalyticsImageUrl, "r");
  $test = fgets($handle);
  echo $test;
  fclose($handle);  

//Pretend this page was a jpg image all along and get the jpg and return it.
$imageurl = fopen ('http://www.default.com/test.jpg', "r"); //this is where the real file should be located
while (!feof ($imageurl)) {
 $image = fgets($imageurl, 4096);
 header('Content-type: image/jpeg');
 echo $image;
}
fclose($imageurl);

Any help would be greatly appreciated as I am very new to GA

A: 

I can't comment on whether it is possible to trigger a request to GA that way, but you seem to have done your homework and the GA call looks fair enough.

To make this work, as far as I can see, the only thing is to not output the GA image's content, as that will screw up your JPG. So remove

 echo $test;

I can't see anything else wrong with it - if it still doesn't work, you'll have to add some info about what the problem is.

Consider though that even if this method works, passing a lot of images through PHP can be a strain on the server, because a PHP interpreter instance is opened for every image request, and caching is going to be a problem if you want to count all views.

Have you considered triggering the GA "hit" event using JavaScript from the parent HTML page? Or is that not reliable enough?

Pekka
I have actually only just added the >>echo $test;the reason for this is that it seems to still not run the GA correctly. My only other thought was to run the fOpen on a separate file in order to run an HTML script with the GA.gif file on it however it doesn't seem to run properly either.I have not thought about the JavaScript option and will need to think about that. Thanks for your help Pekka
NWhite
@NWhite maybe GA is not the right thing for this. Hits on images are logged automatically by your web server. Maybe the web server's logs are the right thing to turn to here.
Pekka