Alec Sanger
Alec Sanger

Reputation: 4562

Injecting <div> for conversion tracking before a redirect

When injecting a div server-side from ASP.NET (using .Visible), is it possible that my pages are occasionally being redirected by the server before the javascript in the injected div block finishes executing? If so, how can I prevent a server-side redirect from occurring before my client-side code is done running?

Specifics of my issue:

I needed a quick solution to conditionally track google conversions on ASP.NET pages right before a redirect. I ended up creating an invisible div for each conversion, and when the submit button was clicked, the correct div block would be made visible from the code-behind, and then the rest of the code would process. A redirect is occasionally only a few lines of code away.

After a few weeks of monitoring the numbers, some aren't adding up properly. For example, on a page that cannot be accessed without a previous conversion happening, I'm getting more conversion on the inner page than the outer (which should not be possible). To make sure this div code was executing every time a submit button was clicked, I added a javascript alert as the last line and it did pop up for me every single time. Even after this testing, though, the only logical explanation I can come up with is that the redirect is occurring before the client-side javascript code fully executes. Since the redirect is happening server-side, these run independently of each other.

Upvotes: 0

Views: 562

Answers (1)

Josiah Ruddell
Josiah Ruddell

Reputation: 29831

This will be extremely unreliable. Most likely the tracking is loading an external script or img. As soon as the redirect begins all active requests from the current page are aborted.

If you must track when the user leaves the page I would try some sort of intermediate tracking landing page, or actually encompass all of the tracking code in the submit client function.

In the latter you would have to load the script/image and only process the submit once the onload callback fires.

Hope this helps.

Upvotes: 1

Related Questions