Search code examples
phpuniversal-analyticsutm

Tracking utm parameters with universal analytics


Previously I was using ga.js (asynchronous - _gaq.push) at time I was 99% able to capture all utm parameters. Later on I have update the code and moved to analytics.js (universal - ga send), from then I am unable to capture a single utm parameter.

Do I need to change the code to to capture utm parameters as now I am using analytics.js ?

My code :

session_start();
$utm_parameters=substr($_REQUEST['__utmz'],strpos($_REQUEST['__utmz'],"utmcsr="));
$utm_source=substr($_REQUEST['__utmz'],strpos($_REQUEST['__utmz'],"utmcsr="));
$utm_source=substr($utm_source,0,strpos($utm_source,"|"));
$utm_source=substr($utm_source,7);
$utm_media=substr($_REQUEST['__utmz'],strpos($_REQUEST['__utmz'],"utmcmd="));
$utm_media=substr($utm_media,0,strpos($utm_media,"|"));
$utm_media=substr($utm_media,7);

if ($_SESSION['url'] != null) {
    $utm_parameters = $_SESSION['url'];
}

if ($_SESSION['utm_source'] != null) {
    $utm_source = $_SESSION['utm_source'];
}

if ($_SESSION['utm_medium'] != null) {
    $utm_media = $_SESSION['utm_medium'];
    // echo 'utm_media='.$_SESSION['utm_medium'];
}

Solution

  • Universal Analytics does not employ the same cookies as Google Analytics. the UTMZ cookie does not exist anymore, and the traffic source you were retrieving before isn't kept in the cookie.

    To overcome this you will need to create your own cookie. Try this project: https://github.com/dm-guy/utm-alternative

    You can also run the old code ga.js side by side with Universal Analytics, but this is not a permanent solution since the support for ga.js will be discontinued at one point.