How to handle Google Analytics tracking code for multiple domains AND where some domains have a different root folder

StackOverflow https://stackoverflow.com/questions/22558973

Вопрос

By using the "setDomainName" property I am able to correctly handle multiple domains. The problem is some domains a different root folders while some have none. For example

http://www.example.com/intro.aspx

http://www.example.com/myexample/intro.aspx

http://www.example.com/YetAnotherExample/intro.aspx

I want hits to intro.aspx to all count as the same.

I know I can apply some filters at the server side things of google analytics. However in my case new domains are being added daily. The problem with that is that 1) server side does not allow post processing of those filters and 2) By the time I figure out what root folders to filter out they are already in my system and the data is messed up.

I'm thinking I need to add something to the tracking code to get it to filter out the extra root folder but I'm not sure how. I'm open to other options as well.

I'm using ASP .NET and the JavaScript Google analytics tracking code Thanks

Это было полезно?

Решение

It is possible to alter the page name value with a filter within the GA interface. If you go to the admin for an account or profile, there's an "all filters" link listed on the left under the account section (if you're on account level), or a "filters" link under the "view" section on the right. Click on one of those (which one depends on how your account/profile/views are setup and what scope you want to apply the filter for), then click on the red "new filter" button.

Then, select "custom filter" for the filter type, and then you can either use the "search and replace" or "advanced" radio button option and work with "request uri". This will change the value before it goes into your reports.

However, this approach is kinda limited in that you need to know on that level what all the root paths will be, and even if you can get a list, the regex will be long and messy and you'll have to keep the list updated.

An alternate (and IMO better) approach would be to put the responsibility on the site owner of the pages. You can override GA's default page name value by specifying it as the 2nd array element in the _trackPageview push:

_gaq.push(['_trackPageview','custom page name here']);

So for example, you do this:

// the root you want to exclude. Make site owners fill this out
var rootPath = '/myexample/'; 

/** now here's logic to specify page name with the root path removed **/
// this replicates GA's default page name
var pageName = location.pathname+location.search;
// replace rootPath with '/'
if (typeof rootPath != 'undefined')
  pageName=pageName.replace( (new RegExp("^"+String(rootPath),"")) , '/' )

// now specify the custom page name 
_gaq.push(['_trackPageview',pageName]);

A word of caution:

I don't know what your exact situation with these "domains" are, but by default GA doesn't report page names with the domain, only the path + query string. So if you have multiple domains all pointing to the same account, pages with similar paths will be aggregated.

Example:

www.site1.com/intro.aspx
www.site2.com/intro.aspx

By default, both of these will show up as the same page in your reports. Removing the "root path" from those urls you showed is only going to exasperate the situation. But maybe this is really what you want to happen anyway, so maybe you're fine with that.

I'm just bringing this up because without additional context, I'm unclear why you would really want to strip the "root path" out of different domains. Usually people have a rollup profile/view that has everything aggregated, and usually people make a filter or otherwise alter the page name to include the domain prefix or similar to page names, so that they don't get rolled up to a single page. But this is all just speculation since you didn't really get into full details about your situation, so you may or may not have addressed that or it may or may not even be applicable to you. .. in any case, what I have shown should work for your immediate issue.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top