Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm trying to do server side Google Analytics tracking and came across php-ga. For some reason this is throwing an error: Parse error: syntax error, unexpected T_STRING, expecting T_CONSTANT_ENCAPSED_STRING or '(' in /home/priedelc/public_html/refll/api_ga.php on line 85 - where 85 is the line with use UnitedPrototype\GoogleAnalytics;

Anyone got a hint for me? Thanks!

//load namespace
use UnitedPrototype\GoogleAnalytics;
//autoload import script
require_once 'autoload.php';

//initialze new tracker & session
$tracker = new GoogleAnalytics\Tracker('xxxx', 'xxxx');
$session = new GoogleAnalytics\Session();
//setup visitor
$visitor = new GoogleAnalytics\Visitor();
$visitor->setIpAddress($_SERVER['REMOTE_ADDRESS']);
$visitor->setUserAgent($_SERVER['HTTP_USER_AGENT']);

//page
$page = new GoogleAnalytics\Page('/page-name');
$page->setTitle('Page Title');
//track pageview
$tracker->trackPageView($page,$session,$visitor);

//event
$event = new GoogleAnalytics\Event();
$event->setCategory('Category');    //string, required
$event->setAction('Action');        //string, required
$event->setLabel('Label');          //string, not required
$event->setValue(1);                //integer, not required
$event->setNoninteraction('true');
//track event
$tracker->trackEvent($event,$session,$visitor);
share|improve this question
2  
What version of PHP are you running? I'm guessing 5.2 or below, before namespaces were introduced – Mark Baker Jun 13 at 16:30

1 Answer

The line looks okay:

use UnitedPrototype\GoogleAnalytics;

Check your PHP version: http://php.net/manual/en/function.phpinfo.php

If your version is below 5.3.0, you need to upgrade your PHP if you'd like to use namespaces: http://www.php.net/manual/en/language.namespaces.rationale.php

Unfortunately I'm not able to test your code with a lower PHP version right now, but I guess it's what you're looking for.

share|improve this answer
Hmm yea, I use 5.2.17 - sadly I can't upgrade it.. shared host.. any workaround? – Paul Riedel Jun 13 at 19:52
I asked Google for a workaround but unfortunately there isn't any. What about outsourcing the file to another server and request it or send data to it by a script on your shared host? It should work, if your mainfocus isn't on performance. – Mr. Bombastic Jun 13 at 21:13
That's a great idea! Sadly it's the only server I can use for this.. Too bad :( Any other solution for server side GA tracking you know of? – Paul Riedel Jun 13 at 21:21
@PaulRiedel check out piwik.org if you're not dependent to Google Analytics. I'm not sure but there could be an GA API for older servers. – Mr. Bombastic Jun 13 at 21:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.