w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
How to handle Google Analytics tracking code for multiple domains AND where some domains have a different root folder

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 

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.



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.

© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.