Using Google Analytics on an image
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"];开发者_JAVA百科
$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
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?
加载中,请稍侯......
精彩评论