php-ga: How to identify organic traffic?

匿名 (未验证) 提交于 2019-12-03 07:50:05

问题:

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?

回答1:

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.



回答2:

is this how Google does it

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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!