Mittwoch, 25. April 2012

Google Analytics without utilizing the clients

If You want to use Google Analytics without JavaScript, You can use the Google or the Galvanize script, but both will utilize the clients by using an Image tag.

The alternative way to avoid this, is a little tweak over the Google Script.
Just follow their instructions and download the ga.php.

You have to comment out:
ga.php line 182:
writeGifData();

Instead of using thier PHP code snipped insert this one before Your own Code:

  // Google Analytics without utilizing the clients
  // URL
  $GA_ACCOUNT = "MO-ACCOUNT_ID";
  $GA_PHP = "ga.php";

  function
sendPageView() {
    global $GA_ACCOUNT, $GA_PHP;
   
    $_GET["utmac"] = $GA_ACCOUNT;
    $_GET["utmn"] = rand(0, 0x7fffffff);
   
    $referer = $_SERVER["HTTP_REFERER"];
    $query = $_SERVER["QUERY_STRING"];
    $path = $_SERVER["REQUEST_URI"];
    if (empty($referer)) {
      $referer = "-";
    }
    $_GET["utmr"] = $referer;
    if (!empty($path)) {
      $_GET["utmp"] = $path;
    }
    $_GET["guid"] = "ON";
   
    include($GA_PHP);
    // Only if You want to debug something
    print_r(error_get_last());
  }
   
  // Avoid sending anything before else You will get:
  // Warning: Cannot modify header information
  sendPageView();
 
  // YOUR PHP CODE GOES HERE
Attention:
Avoid sending anything before else You will get the warning:
Cannot modify header information

More advanced Version:
You have to comment out also:
ga.php line 154 - 158: 
setrawcookie(...)
And comment out at the end of the file:
trackPageView();

Now You can send it also after Your code, just call:
trackPageView();
Maybe You want also to rename our function sendPageView to initGoogleAnalytics, because it will never send anything.

Further:
If You want to use the whole Google Analytics functionalities, You may take a look on the Server-Side Google Analytics PHP Client

Keine Kommentare:

Kommentar veröffentlichen