问题
I run a standard google analytics tracking code (ga.js asynch version) on a website. I am wondering if there is a line of js I can add to the tracking code so that I can segregate dev/prod data? e.g. when I pull in the codebase to do dev work, I can set the tag to DEV. When releasing back to production, DEV tag gets replaced with PROD tag.
Is this even possible and if so, how do i implement it?
One method I thought of is just to create a new "property" (which would generate a new ua number, which I could add during dev. that would allow me to track it all separately.)
Wondering if there are any other methods I should consider.
回答1:
I'm not worried about collecting data from my development server, but I do want to make sure I'm not polluting my production data -- I've been using some variation of the following:
if (!/devServer|localhost/.test(window.location.hostname))
{
_gaq.push(['_setAccount', 'UA-11111111-1']);
}
On the devserver domain (or on localhost), _setAccount
doesn't get executed, so the tracker defaults to the default tracker, UA-99999999-1
. This allows you to still see tracking data being sent to the analytics servers (via ga_debug.js, chrome dev tools, firebug, fiddler, etc), but doesn't register against your production profiles.
Downsides -- it is an extra bit of code that get's run on the client.
If you do want analytics from your development servers, you could try something like:
gaq.push(
[ '_setAccount',
/devServer|localhost/.test(window.location.hostname) ? 'UA-11111111-1',
'UA-22222222-1']
);
回答2:
Alternative to mike's answer is to setup a filter in your profile based on the url or domain or based off any number of other things.
来源:https://stackoverflow.com/questions/14295311/google-analytics-tag-for-dev-vs-prod