I'm doing all my Google Analytics server side, but GA is only tracking direct or referrals, and I don't know how to track organic. This is a pice of code that gets either direct or referral:
//Campaign is used for the referrals
//If not in session and there is a referrer, create campaign from referrer
//and add it to the tracker and to session.
if (!isset($_SESSION['campaign'])) {
if (isset($_SERVER['HTTP_REFERER']) &&
strpos($_SERVER['HTTP_REFERER'], parse_url($this->config['url']['base'], PHP_URL_HOST)) === FALSE) {
$campaign = GoogleAnalytics\Campaign::createFromReferrer($_SERVER['HTTP_REFERER']);
$this->tracker->setCampaign($campaign);
$_SESSION['campaign'] = serialize($campaign);
}
} else {
//If already in session, add it to the tracker
$this->tracker->setCampaign(unserialize($_SESSION['campaign']));
}
The above basically analyzes the referer; if from another source, creates a referral, if not it doesn't. Then it is stored in the session if there was a referral.
Now, how would I identify organic sources? I was thinking on making a table of possible organic sources, is this how Google does it? Something like:
protected $organic_sources = array('www.google.com', 'www.yahoo.com')
Then I would check the source in there before creating the campaign, if in array I would create it as an organic campaign. Is this an optimal solution? Any thoughts on how to identify organic traffic?
Basically yes - as far as GA is concerned an organic search visits is a referer from a known (by url) search engine plus a search parameter (to grab the search keyword) but without utm- or glcid-parameters (which would turn the referer into a campaign url). In client-side GA you can even add your own set of search engines, so I'd say that's the way it should work for the server side, too.
Yes, that's how Google does it. I created a small function to identify organic traffic. It goes like this:
Just put the above in your class, and add this function:
You can then just call the function above by passing
$_SERVER['HTTP_REFERER']
as param. Hope it's useful for someone.