Reputation: 2002
I have a short application form, where I capture some information, then redirect the user to a third party website where they complete the application.
I am using Google Analytics (and a Google conversion tracker) to monitor the hits on our site. Therefore I have to direct to a "conversion page" with the Analytics Javascript and a redirect. Unfortunately my javascript is not getting called.
How would I ensure the Javascript is called before the redirect happens?
<head runat="server">
<title></title></head><body>
<script type="text/javascript" src="/scripts/Analytics.js"></script>
<script type="text/javascript" src="/scripts/Analytics2.js"></script>
<!-- Google Code for Filling out Form Conversion Page -->
<script type="text/javascript">
/* <![CDATA[ */
var google_conversion_id = 964293672;
var google_conversion_language = "en";
var google_conversion_format = "2";
var google_conversion_color = "ffffff";
var google_conversion_label = "H0y7CMi73wIQqOjnywM"; var google_conversion_value = 0;
/* ]]> */
</script>
<script type="text/javascript" src="http://www.googleadservices.com/pagead/conversion.js">
</script>
<noscript>
<div style="display: inline;">
<img height="1" width="1" style="border-style: none;" alt="" src="http://www.googleadservices.com/pagead/conversion/964293672/?label=H0y7CMi73wIQqOjnywM&guid=ON&script=0" />
</div>
</noscript>
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Page.LoadComplete += new EventHandler(Page_LoadComplete);
}
}
void Page_LoadComplete(object sender, EventArgs e)
{
Response.Redirect("https://anotherwebsite.com/c/transint?uri_b=ia_86894");
}
Upvotes: 2
Views: 2338
Reputation: 66641
You can get the mobile code from Google Analytics and make an internal direct call to Google Analytics just before the redirect.
See the code example here. http://code.google.com/mobile/analytics/docs/web/
from your analytic account download the ga.aspx file and change it according to your account to call Google Analytics using code behind.
Here is that part of code that you need to focus.
string utmGifLocation = "http://www.google-analytics.com/__utm.gif";
// Construct the gif hit url.
string utmUrl = utmGifLocation + "?" +
"utmwv=" + Version +
"&utmn=" + GetRandomNumber() +
"&utmhn=" + HttpUtility.UrlEncode(domainName) +
"&utmr=" + HttpUtility.UrlEncode(documentReferer) +
"&utmp=" + HttpUtility.UrlEncode(documentPath) +
"&utmac=" + account +
"&utmcc=__utma%3D999.999.999.999.999.1%3B" +
"&utmvid=" + visitorId +
"&utmip=" + GetIP(GlobalContext.Request.ServerVariables["REMOTE_ADDR"]);
SendRequestToGoogleAnalytics(utmUrl);
private void SendRequestToGoogleAnalytics(string utmUrl)
{
try
{
WebRequest connection = WebRequest.Create(utmUrl);
((HttpWebRequest)connection).UserAgent = GlobalContext.Request.UserAgent;
connection.Headers.Add("Accepts-Language",
GlobalContext.Request.Headers.Get("Accepts-Language"));
using (WebResponse resp = connection.GetResponse())
{
// Ignore response
}
}
catch (Exception ex)
{
if (GlobalContext.Request.QueryString.Get("utmdebug") != null)
{
throw new Exception("Error contacting Google Analytics", ex);
}
}
}
Maybe this is looks like Google hack, I do not know. The other way is to use the Google Analytics SDK to see there is there is any other official way to call it
http://code.google.com/apis/analytics/docs/tracking/home.html
Upvotes: 2
Reputation: 2002
I have a third tracking script I need to add so I just used a JavaScript redirect in a jQuery docReady function.
I used window.location.replace(...), as it doesn't put the redirect page in browsing history.
Thanks for your input @Aristos.
Upvotes: 0