2013-07-02 126 views
0

首先,我是PHP新手。 我想建立PDF因爲我在使用這種方法的工作網站追蹤: http://www.lunametrics.com/blog/2013/06/04/tracking-pdfs-google-analytics-server-side/使用php-ga跟蹤pdf和Google Analytics(分析)無法正常工作。

我已經實現的代碼,但我得到一個錯誤

Parse error: syntax error, unexpected T_STRING, expecting T_CONSTANT_ENCAPSED_STRING or '(' in /home/ngjge/public_html/download.php on line 9

任何幫助或指針會不勝感激...

爲的download.php頁我的PHP代碼:

?php 

// Set header MIME-Type for PDF 
header("Content-type: application/pdf"); 

// Google Analytics Server Side 
$GA_ACCOUNT = "UA-8496414-14"; // replace with your GA-ID 
include "autoload.php"; 
use UnitedPrototype\GoogleAnalytics; 

// Initilize GA Tracker 
$tracker = new GoogleAnalytics\Tracker($GA_ACCOUNT, 'goodandevilbook.com'); 

// Assemble Visitor information 
$visitor = new GoogleAnalytics\Visitor(); 
$visitor->setIpAddress($_SERVER['REMOTE_ADDR']); 
$visitor->setUserAgent($_SERVER['HTTP_USER_AGENT']); 
$visitor->fromUtma($_COOKIE['__utma']); 
//$visitor->setScreenResolution('1480x1200'); 

// Assemble Session information 
$session = new GoogleAnalytics\Session(); 
$session->fromUtmb($_COOKIE['__utmb']); 

// Get filename from the previous request 
$filename = parse_url(urldecode($_SERVER['REQUEST_URI']), PHP_URL_PATH); 
//$filetype = preg_replace("/.+\.(.+)/i","$1",$filename); 

// Assemble Page information 
$page = new GoogleAnalytics\Page($filename); 
$page->setTitle($filename); 
$page->setReferrer($_SERVER['HTTP_REFERER']); 

// Track page view 
$tracker->trackPageview($page, $session, $visitor); 

// Create the URL for the PDF 
$protocol = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') ||  $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://"; 
$url = $protocol.$_SERVER['HTTP_HOST'].$filename; 

// Fetch the PDF (cURL it) 
$ch = curl_init($url); 
// This creates a user-agent string that we set .htaccess to ignore (preventing an endless ##loop) 
curl_setopt($ch, CURLOPT_USERAGENT, "LunaMetrics123"); 
$data = curl_exec($ch); 
curl_close($ch); 

// For good measure 
exit; 
?> 

回答

0

你」可能不會使用PHP 5.3或更高版本。

0

我意識到這可能是老了......但是:

我跑這一個PHP 5.3的服務器上:

spl_autoload_register(function($className) { 
    if($className[0] == '\\') { 
     $className = substr($className, 1); 
    } 

    // Leave if class should not be handled by this autoloader 
    if(strpos($className, 'UnitedPrototype\\GoogleAnalytics') !== 0) return; 

    $classPath = strtr(substr($className, strlen('UnitedPrototype')), '\\', '/') . '.php'; 

    if(file_exists(__DIR__ . $classPath)) { 
     require(__DIR__ . $classPath); 
    } 
}); 




use UnitedPrototype\GoogleAnalytics; 

// Initilize GA Tracker 
$tracker = new GoogleAnalytics\Tracker('UA-103xxxxx', 'server.com'); 

// Assemble Visitor information 
// (could also get unserialized from database) 
$visitor = new GoogleAnalytics\Visitor(); 
$visitor->setIpAddress($_SERVER['REMOTE_ADDR']); 
$visitor->setUserAgent($_SERVER['HTTP_USER_AGENT']); 
$visitor->setScreenResolution('1024x768'); 

// Assemble Session information 
// (could also get unserialized from PHP session) 
$session = new GoogleAnalytics\Session(); 

// Assemble Page information 
$page = new GoogleAnalytics\Page($_SERVER["SCRIPT_FILENAME"]); // done 
$page->setTitle('My Page'); 

// Track page view 
$tracker->trackPageview($page, $session, $visitor); 

http://eamann.com/tech/server-side-analytics-google/ 以下鏈接我在這裏登陸:https://github.com/thomasbachem/php-ga/

的代碼的第一部分來自'autoload.php'頁面,我發現它在初始化之前是必需的。

相關問題