Know where the user came from

    When you open your project, you begin to write about it everywhere, including on the Habr .

    Statistics services such as Google Analytics will give you an overall estimate of the traffic, namely how many visitors came from which resource. You can set up goals and track registrations or purchases, but this is often not enough.

    But what if you need statistics about where the most active users are coming from or users who created the most topics on the forum for a certain period of time, or made the most purchases in your online store. There can be many options, and such data analytics services will not be able to give.



    To solve this problem, we just need to save data about the resource where the user came from when registering.

    This data can be obtained, for example, from __utmz Google Analytics cookies and written to some field in the database.

    The __utmz cookie value usually looks something like

    264345247.1261843448.2.3.utmcsr = habrahabr.ru | utmccn = (referral) | utmcmd = referral | utmcct = / blogs / i_am_advertising / 63791 / This code will split the __utmz cookie value into pairs and write it in an associative array. Now, when registering a user, you can get this data and record with a new user. We took utmcsr and utmcct which store the host and url of the linking page (there can be more than one page, therefore we write separately). Now all the data is stored in the database and any statistics can be collected with simple SQL queries.

    static function parseGoogleAnalyticsCookies(){
    $returnMap = array();
    $cookieVal = $_COOKIE["__utmz"];
    //now split cookie value by |
    $arrPairs = explode('|', $cookieVal);
    foreach($arrPairs as $pair){
    $pair = explode('=', $pair);
    if (sizeof($pair) == 2){
    $key = $pair[0];//look for "."
    if (strpos($key, ".")){
    $key = substr($key, strrpos($key, ".")+1 );
    }

    $returnMap[$key] = $pair[1];
    }
    }
    return $returnMap;
    }






    $newUser = $model->create();
    //..... утснанавливаем имя/хэш пароля и тп
    $sourceCookiesData = GoogleAnalyticsCookies::parseGoogleAnalyticsCookies();//GoogleAnalyticsCookies назван для примера
    if (isset($sourceCookiesData['utmcsr'])){
    $newUser->source = $sourceCookiesData['utmcsr'];
    }
    if (isset($sourceCookiesData['utmcct'])){
    $newUser->sourceUrl = $sourceCookiesData['utmcct'];
    }
    $newUser->save();






    In such a simple way, you can find out where more targeted traffic is coming from and where to direct your efforts in promotion.

    Also popular now: