dbers
dbers

Reputation: 654

track page visit as an alias

I've been trying to find a solution for this but haven't seen anything useful google'ing around. The issue might be that i'm using terminology that has other meanings.

I have a site that has utilizes the same URL's for both guests and members. In order to track visits and goals correctly i need a way to tell if the visitor is a logged in user or a guest.

The easiest way i can think of is if i just prefix my URL's with something like /member/

I thought maybe i could do this in the google tracking code by adding this:

_gaq.push(['_trackPageview'], '/member' + window.location.pathname);

the full code is:

<script type="text/javascript">
    var _gaq = _gaq || [];
    _gaq.push(['_setAccount', 'xxxxxxxxxx']);
    _gaq.push(['_trackPageview'], '/member' + window.location.pathname);

    (function() {
        var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
        ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
    })();
</script>

However this isn't working, also reading the google analytic docs, this seems to be a way to add an 'additional' page view.

I can update the servers code to prefix the urls with /member/ and then strip it before processing the path for the controllers, but i would rather not risk breaking a stable system thats about to go live.

Does anyone know how this can be done in JS before the normal URL is tracked?

Thanks, Dan

Upvotes: 1

Views: 900

Answers (2)

Yahel
Yahel

Reputation: 37305

You have a minor syntax error with your _trackPageview call.

It should just be this:

_gaq.push(['_trackPageview', '/member' + window.location.pathname]);

Note that the /member+ part is now within the array, not outside of it.

What you were doing was passing the _trackPageview function name without a second parameter to be its argument (which, by default, tracks the pageview as location.pathname+location.search, and then passing a string /member+window.location.pathname to the array, which did nothing.

Passing the custom URL as a 2nd item in an array will make it work just fine.

Upvotes: 3

Ciaran
Ciaran

Reputation: 1904

Aside from the fact that you probably shouldn't use the same URL structure for goal tracking in Google Analytics, I can suggest a couple of ways to solve the problem. Note that this doesn't address the alias-tracking question you asked, but offers some alternatives.

1. Append a querystring parameter to goal pages for members or guests

Bit of a hack, but it should be trivial to append a ?guest=true param if your users are not logged in. This would be a minor change that should not affect your business logic or website operation.

You can then set different goals in GA that look for the success page with or without the URL param.

2. Set custom variables in Google Analytics for members and guests

This is the more correct solution.

Use session-level custom variables to distinguish different visitor experiences across sessions. For example, if your website offers users the ability to login, you can use a custom variable scoped to the session level for user login status. In that way, you can segment visits by those from logged in members versus anonymous visitors.

Then you can segment your goal starts and completions using your advanced segments, segmenting on user login status:

Upvotes: 1

Related Questions