Track campaigns with Google Analytics without query string parameters?

后端 未结 9 1182
忘掉有多难
忘掉有多难 2020-11-30 18:52

Is there a supported way in Google Analytics to track a campaign without having to use query string parameters.

In Analytics you can tag a link to your site with que

相关标签:
9条回答
  • 2020-11-30 19:18

    _set campaignParams

    Your theoretical "_setCampaignData" finally exists, in the form of ["_set","campaignParams",...]

    If you have a way to programmatically inject the values you'd like to set (for example, set by a cookie on a redirect, or on the server side and printed onto the page), you can use the _set API to hard-code the campaign params that you'd like to set.

    The format for that is just:

    _gaq.push(['_set', 'campaignParams', 
    'utm_campaign=CAMPAIGN&utm_source=SOURCE&utm_medium=MEDIUM']);
    

    So, using your original example:

     var campaignMedium = <%= ViewData.Model.CampaignMedium %>;
     var campaignSource = <%= ViewData.Model.CampaignSource %>;
     var campaignName = <%= ViewData.Model.CampaignName %>;
     _gaq.push(['_set', 'campaignParams', 
    'utm_campaign=' + campaignName +  '&utm_source=' + campaignSource +'&utm_medium=' + campaignMedium]);
    

    Update 2017

    This answer details how to accomplish this with the newer Google Analytics library, analytics.js/Universal Analytics.

    0 讨论(0)
  • 2020-11-30 19:25

    The new(er) universal analytics allows you to specify these params - see the documentation here https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference

    Look for the "Campaign Source" section for an example of setting what would be the utm_source param.

    0 讨论(0)
  • 2020-11-30 19:27

    There is a function _setAllowAnchor in the Trackin API that allows you to specify the tags in the anchor text instead of as query parameters.

    pageTracker._setAllowAnchor(true);
    

    So you can use http://www.stackoverflow.com/#utm_source=expertexchange and GA will understand it. This way you can avoid the SEO problem.

    For the Twitter problem, I suggest you the method described in post Tracking Twitter and Shorten URLs in Google Analytics.

    0 讨论(0)
  • 2020-11-30 19:28

    Below is an updated way to do this using the Google Analytics Universal method of Event tracking rather than GA Standard Event tracking.

    The entire UTM string can be extracted from the queryString (when there) or pulled from the Google cookie (__utmz) and then passed into Google Analytics using the below code.

    ga('send', 'event', 'queryString', 'getQueryString', googleString ); 
    

    Then, upon execution of whatever trigger you want to create (I use a function named "widgetTracker"), you can execute the GA event tracker.

    Here's the entire code:

    /* JavaScript Document */
    (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
      (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
      m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
      })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
      ga('create', 'UA-xxxxxxx-xx', 'xx.xxx');
      ga('send', 'pageview');
    
     var googleString;  
     var stringArray = [];  
     var queryStringObject = makeQueryStringObject();   
     var QUOT = "'";    
     var EQ = '=';  
     var AMP = '&';         
    
     for  ( var v in queryStringObject ) {  
        var str =  v + EQ + queryStringObject[v] ;  
        stringArray.push(str);   
        googleString = stringArray.join(AMP);
    } 
    
             function makeQueryStringObject()   {       
                var obj = [];       
                var pageURL = window.location.href;         
                var URLArray = pageURL.split('?');      
                if( URLArray[1] )       {           
                    var argsArray = URLArray[1].split('&');             
                    var l = argsArray.length;           
                    for( i=0; i<l; i++ )            {               
                        var individualArg = argsArray[i].split('=');                
                        if(individualArg[1] && individualArg[1].indexOf('#') > -1)              {               
                            var dropHashArray = individualArg[1].split('#');                    
                            individualArg[1] = dropHashArray[0];                
                        }               
                        obj[ individualArg[0] ] = individualArg[1];     
                        console.log("value of queryStringObject: " + individualArg[0] + " :: " + obj[   individualArg[0] ]);
    
                    }       
                } else { /* from http://stackoverflow.com/a/14984832/1013405 */         
                    ga1 = parseGACookie();  
                    if(ga1['utmcsr']) {             
                        var utm_source = ga1['utmcsr'];                     
                    }           
                        if(ga1['utmccn']) {             
                            var utm_campaign = ga1['utmccn'];                   
                        }           if(ga1['utmcmd']) {             
                            var utm_medium = ga1['utmcmd'];                 
                            }           
                            if(ga1['utmctr']) {             
                                var utm_term = ga1['utmctr'];                           
                            }           
                            if(ga1['utmcct']) {             
                                var utm_content = ga1['utmcct'];                
                            }       
                        }   
                            var googleString2 = "utm_campaign=" + utm_campaign + '&' + "utm_medium=" + utm_medium + '&' + "utm_term=" + utm_term + '&' + "utm_content=" + utm_content;  
                        return obj;     
                    } 
    
                    function parseGACookie()  {     
                    var values = {};     
                    var cookie = readCookie("__utmz");     
                    if (cookie) {         
                        var z = cookie.split('.');         
                        if (z.length >= 4) {             
                            var y = z[4].split('|');            
                             for (i=0; i<y.length; i++) {                 
                                var pair = y[i].split("=");                 
                                values[pair[0]] = pair[1];             
                             }         
                            }     
                        }     
                        return values; 
                    }  
    
    
                    function readCookie(name) {     
                        var nameEQ = name + "=";     
                        var ca = document.cookie.split(';');     
                        for(var i=0;i < ca.length;i++) {        
                         var c = ca[i];        
                          while (c.charAt(0)==' ') c = c.substring(1,c.length);         
                          if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);     
                        }     
                        return null; 
                    }  
                    function widgetTracker() {  
                    /* event tracking order of elements: send (required), event (required), Category, Action, Label, Value */ 
                        if (googleString) { 
                        ga('send', 'event', 'queryString', 'getQueryString', googleString ); 
                        } else { 
                         ga('send', 'event', 'queryString2', 'getQueryString2', googleString2 ); 
                         }
                    }  
    
    0 讨论(0)
  • 2020-11-30 19:29

    Török Gábor gave me an idea.

    // ...
    var campaignMedium = <%= ViewData.Model.CampaignMedium %>;
    var campaignSource = <%= ViewData.Model.CampaignSource %>;
    var campaignName = <%= ViewData.Model.CampaignName %>;
    
    // save the old hash
    var oldHash = document.location.hash;
    
    // add campaign data to the hash
    document.location.hash = 'utm_source=' + escape(campaignSource) + ...;
    pageTracker._setAllowAnchor(true);
    pageTracker._trackPageview();
    // restore the old hash:
    document.location.hash = oldHash;
    

    This way, you could create the campaign data in the backend, and then, pass it to the hash dynamically, and then restore it without user even noticing it. I.e. the campaign tracking is 100% independent of the real URL.

    0 讨论(0)
  • 2020-11-30 19:29

    Well, I didn’t test it, but try to add those params to the .trackPageview() method, i.e.:

    pageTracker._trackPageview("?utm_source=...")
    

    Anyways, You have to pass those parameters some how. And this means, you’ll allways have long URL-s with some kind of campaign crap in them. It won’t prevent bookmarking the wrong URL-s, search engines indexing them, etc.

    If you want to maintain control of those parameters, setup separate URL-s for partners, that redirects to the tagged target URL:

    http://example.com/campaigns/1 -> http://example.com/?utm_source=...
    
    0 讨论(0)
提交回复
热议问题