barryokane
11/26/2013 - 8:17 AM

Google Analytics Cross domain tracking when using server side redirect

Google Analytics Cross domain tracking when using server side redirect

<%@ Page Language="C#" %>

<script runat="server">
    
    protected void RedirectClick(object sender, CommandEventArgs e)
    {
        if (Page.IsValid)
        {
            string url = RedirectUrl.Value; 
            //... whatever else ...
            Response.Redirect(url);
        }
    }
</script>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script type="text/javascript">

        var _gaq = _gaq || [];
        //...YOUR ASYNC GOOGLE ANYLTICS CODE HERE ...
   
        function AppendGA() {
            var tracker = _gaq._getAsyncTracker('rollup');
            $("#<%= RedirectUrl.ClientID %>").val(tracker._getLinkerUrl($("#<%= RedirectUrl.ClientID %>").val()));
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
        ... other form controls including buttons with different server side events ..
        <div>
        
        ...controls relevant to a server side redirect ...
        
        <asp:HiddenField ID="RedirectUrl" Value="http://test.com" runat="server" />
        <asp:Button ID="submit" runat="server" Text="Redirect Event" OnCommand="RedirectClick" ValidationGroup="Group2" OnClientClick="AppendGA()"></asp:Button>
     
        </div>

    </form>
    
</body>
</html>