Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

Cross-post from Stackoverflow

Here's the problem I am having:

Currently, we need to track across www.chess.com and secure.chess.com. However, I cannot set the domain to .chess.com because the cookie causes some problems on another domain, live.chess.com.

So, to work around this, in code - I do something like this:

<? if (HTTPS) { ?>
    <script>
    var _gaq = _gaq || [];
    _gaq.push(['_setAccount', 'UA-XXXXX-01']);
    _gaq.push(['_setDomainName', 'secure.chess.com']);
    _gaq.push(['_trackPageview']);
    </script>
<? } else { ?>
    <script>
    var _gaq = _gaq || [];
    _gaq.push(['_setAccount', 'UA-XXXXX-02']);
    _gaq.push(['_setDomainName', 'www.chess.com']);
    _gaq.push(['_trackPageview']);
    </script>
<? } ?>

However, this forces us to maintain 2 analytics accounts (UA-XXXXX-01 and UA-XXXXX-02) and creates a separation between HTTPS and HTTP traffic.

How can I set this up so that ONE analytics account will track both secure.chess.com and www.chess.com without setting a top level cookie (.chess.com)?

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted
<? if ( %%HTTP_HOST%% != 'live.chess.com' ) { ?>
<script>
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXX-01']);
_gaq.push(['_setDomainName', '<? echo %%HTTP_HOST%% ?>']);
_gaq.push(['_trackPageview']);
</script>
<? } ?>

(Where %%HTTP_HOST%% is a variable generated by your server-side script)

Edit: To confirm, the following code is included on all pages generated at secure.chess.com and www.chess.com and no script errors display at secure.chess.com correct?

<script type="text/javascript">
  (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>
share|improve this answer
    
I have tried this approach, and it still only tracks www.chess.com and not ever secure.chess.com :\ –  leek Nov 7 '10 at 5:06
    
@leek - Updated per comments –  danlefree Nov 7 '10 at 6:27
    
My apologies, your original answer was correct. Apparently I hadn't waited long enough for GA to update. Thanks! –  leek Nov 8 '10 at 19:13
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.