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?
Yes, that's how Google does it. I created a small function to identify organic traffic. It goes like this:
/*
* Organic sources
*/
protected $organic_sources = array('www.google' => array('q='),
'daum.net/' => array('q='),
'eniro.se/' => array('search_word=', 'hitta:'),
'naver.com/' => array('query='),
'yahoo.com/' => array('p='),
'msn.com/' => array('q='),
'bing.com/' => array('q='),
'aol.com/' => array('query=', 'encquery='),
'lycos.com/' => array('query='),
'ask.com/' => array('q='),
'altavista.com/' => array('q='),
'search.netscape.com/' => array('query='),
'cnn.com/SEARCH/' => array('query='),
'about.com/' => array('terms='),
'mamma.com/' => array('query='),
'alltheweb.com/' => array('q='),
'voila.fr/' => array('rdata='),
'search.virgilio.it/' => array('qs='),
'baidu.com/' => array('wd='),
'alice.com/' => array('qs='),
'yandex.com/' => array('text='),
'najdi.org.mk/' => array('q='),
'aol.com/' => array('q='),
'mamma.com/' => array('query='),
'seznam.cz/' => array('q='),
'search.com/' => array('q='),
'wp.pl/' => array('szukai='),
'online.onetcenter.org/' => array('qt='),
'szukacz.pl/' => array('q='),
'yam.com/' => array('k='),
'pchome.com/' => array('q='),
'kvasir.no/' => array('q='),
'sesam.no/' => array('q='),
'ozu.es/' => array('q='),
'terra.com/' => array('query='),
'mynet.com/' => array('q='),
'ekolay.net/' => array('q='),
'rambler.ru/' => array('words=')
);
Just put the above in your class, and add this function:
/*
* Check if source is organic
*
* @param string $referrer The referrer page
*
* @return true if organic, false if not
*/
public function isTrafficOrganic($referrer) {
//Go through the organic sources
foreach($this->organic_sources as $searchEngine => $queries) {
//If referrer is part of the search engine...
if (strpos($referrer, $searchEngine) !== false) {
//Check if query is also there
foreach ($queries as $query) {
if (strpos($referrer, $query) !== false) {
//If there, traffic is organic
return true;
}
}
}
}
return false;
}
You can then just call the function above by passing $_SERVER['HTTP_REFERER']
as param. Hope it's useful for someone.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With