Find your content:

Search form

You are here

Javascript redirect with Google Analytics cross-domain link


I have a visualforce site, running the Google Analytics asynchronous tracker. So that a customer can log in, we have a redirect from the unsecure, branded site ( to the domain.

Works fine, but breaks our Analytics source tracking (cookie set by custom domain can't be read by the cookie on So we use the GA cross-domain tracking method _link('url').

Works for Chrome, but not for IE. In this, a controller is supplying the value of {https} based on a custom setting.

      var registerRedir = '{!https}';         
      _gaq.push(['_link', '{!https}']);              
      window.location.href = registerRedir;        

Attribution to: Shane McLaughlin

Possible Suggestion/Solution #1

I did get this one solved. 3 nasty steps, but the cookie stays on the visitor the entire time.

1) use a remote method for the login that returns the "frontdoor" url

    global static string RemoteLogin(string email, string password, string url){
        PageReference result = new PageReference('temp');
        try {
            result = Site.login(email, password, url);
        }catch (exception e){
            return e.getMessage();
        if (result!=null) {
            return result.getURL(); 
        } else {
            return 'login error';

2) use the normal GA push link method to get to your site login page (on the secure side). Could be a link or a button, or a redirect. In our case, we did a redirect. Make sure your GA script happens before the push method just in case.

<apex:outputpanel rendered="{!$Site.CurrentSiteUrl=''}">
    <c:GoogleAnalyticsTrackingCodeScript />
    _gaq.push(['_link', '']);

The "real content" of the login page is in another rendered tag that shows if you're already on the secure domain.

If you did this right, you'll see the page redirect and all the GA UTM_* cookies will be attached as url parameters.

Step 3: Call your remoting login method and parse what comes back

function Login(){
    var pagename = '{!$CurrentPage.Name}';
    if (pagename == 'SiteLogin') pagename='homepage';
    ControllerName.RemoteLogin($j("#email").val(),$j("#password").val(), '/'+pagename, function(result2, event){
        if (result2!='login error'){
            var decoded = result2.replace('amp;', '').replace('amp;', '').replace('amp;', '').replace('amp;', '').replace('amp;', '').replace('amp;', '').replace('amp;', '').replace('amp;', '');                                    
            _gaq.push(['_link', decoded]);
        } else{


More on the GA push link method:

Attribution to: Shane McLaughlin

Possible Suggestion/Solution #2

Just glancing here:

I think the cross-domain tracking is breaking because your actually not directing them to another top-level domain ( is a sub-domain of, and it seems they intended this for use in tracking sub-domains within the same space as a top-level domain, or two-top level domains. Not a top-level and the sub-level of another?

Attribution to: jordan.baucke
This content is remixed from stackoverflow or stackexchange. Please visit

My Block Status

My Block Content