Extracting campaign data from Google Analytics cookies

Google Analytics in the last couple of years became a standard tool for web analytics as well a statndard tool for tracking marketing campaign. when a visitor hits first time your site Google Analytics sets different types of cookies in the visitors browser, one of these cookies is called utmz. this cookie holds quite valuable information like campaign name, campaign source, campaign medium, campaign term and campaign content.

to extract this data I used the following PHP method :

function getUtm(){
    $utm = array();
    if(!empty($_COOKIE['__utmz'])){
        $pattern = "/(utmcsr=([^\|]*)[\|]?)|(utmccn=([^\|]*)[\|]?)|(utmcmd=([^\|]*)[\|]?)|(utmctr=([^\|]*)[\|]?)|(utmcct=([^\|]*)[\|]?)/i";
        preg_match_all($pattern, $_COOKIE['__utmz'], $matches);
        if(!empty($matches[0])){
            foreach($matches[0] as $match){
                $pair = null;
                $match = trim($match, "|");
                list($k, $v) = explode("=", $match);
                $utm[$k] = $v;
           }
        }
    }
    return $utm;
}

the returned array from this method will contain the following:
utmccn => campaign name
utmcsr => campaign source
utmcmd => campaign medium
utmctr => campaign term or keyword
utmcct => campaign content

6 thoughts on “Extracting campaign data from Google Analytics cookies”

  1. Are you able to link the data with the user?
    i.e.:
    Can I see in CRM – for each user- the below details?
    1. User came from the “Buy 1 get 1 Free” campaign (name)
    2. From source: Facebook (source)
    3. It was a 300×250 banner (medium)
    4. Targeting Novice traders (content)

  2. Yes I am already gathering this data and submitting it directly to Sales Force , as well I get organic keywords that every user used to get to the web site and save it into CRM.

  3. I have a question. I figured this group might be a good place to ask it. I have been using Google Analytics for a while. For one of my customers we use campaign tracking extensively. And recently we started collecting Multi Channel Funnel analytics. The primary reason for using it is so we can tracking which campaigns people are coming from and then when someone fills out an inquiry form, we have a goal set up (the inquiry thank you page), so Google Analytics can associate the goal with the campaign(s) that let that person to the thank you page.
    My customer asked me today, “That information is great and extremely valuable. What would make it even better is if when the visitor arrived at the inquiry form, there was a way to extract the campaign information from the analytics cookie and put that into a hidden field on the form…so when the form is submitted, the sales person that receives it not only sees the information the visitor filled out, but they also have a field that shows which campaign(s) let that person to ultimately fill out the inquiry form.” That was not verbatim, but hopefully you understand what they are asking for.
    I know that the cookies must track all of the campaigns because Google Analytics shows that information in the reports. It’s automatically pulling out that information when someone lands on the inquiry form page and putting it into a hidden field (they don’t want the customer seeing that information) so the sales department can see it that will really complete the report system.
    If anyone knows how to do this or has any insights that would be awesome. Thanks.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>