thomasbachem / php-ga

Server-Side Google Analytics PHP Client
Other
317 stars 100 forks source link

Code Example: setcookie and increment visitCount #3

Open mahal opened 11 years ago

mahal commented 11 years ago

it's a great library, just implemented it for our xml-newsfeed transported to a Mac App.

Biggest step was to implement the returning visitor count.

Now this is either a feature request for php-ga or for it's documentation:

Why would I have to do the increment of returning visits in my code? Why isn't this done inside tracker?

Now: $isNewVisitor = true; if ($_COOKIE['__utma']) { $visitor->fromUtma($_COOKIE['__utma']); $isNewVisitor = false; }

if($isNewVisitor) {
    // all defaults are set when Visitor is inited
    // dont know how to get account hash from tracker...
    // $utmaContent = array( '217597461', $visitor->getUniqueId(), $visitor->getFirstVisitTime()->getTimeStamp(), $visitor->getPreviousVisitTime()->getTimeStamp(), $visitor->getCurrentVisitTime()->getTimeStamp(), $visitor->getVisitCount());
} else {
    $visitor->setVisitCount( $visitor->getVisitCount() + 1);
}

$utmaContent = array( '217597461', $visitor->getUniqueId(), $visitor->getFirstVisitTime()->getTimeStamp(), $visitor->getPreviousVisitTime()->getTimeStamp(), $visitor->getCurrentVisitTime()->getTimeStamp(), $visitor->getVisitCount());
$utmaContent = implode('.', $utmaContent);

$cookieSet = setcookie('__utma', $utmaContent, strtotime('+2 years'));

New: