If you use Google Analytics, the postback fails on the ASP.NET page

I use ASP.NET to create a small web application. One of my pages includes some LinkButton controls that worked fine until I added a link to the Google Analytics code . Now, when I click the link button, I get an error message:

Microsoft JScript runtime error: '__doPostBack' property value is null or undefined, not a function or object

Other links and controls on the page work fine. If I remove the link to the Google Analytics script from the page, everything will be fine. It seems that the problem is due to the interaction between the Google Analytics script and LinkButton controls that are trying to bring the page back.

UPDATE I also noticed the following. If the Google Analytics script link is missing, the HTML generated by ASP.NET looks fine:

HTML structure with no Google Analytics code

However, as soon as I add the Google Analytics code, the HTML twists:

HTML structure with Google Analytics code

Verify that the form tag! Now I assume that the opposite error occurs because the linkbutton controls are located outside the ASP.NET form. But why? END UPDATE .

Any ideas on how to solve this? Thanks.

ADDITIONAL UPDATE . After much experimentation, I was able to solve this myself. I added the answer below, showing my findings. Thanks to everyone who posted the answers here. END UPDATE .

+4
source share
5 answers

After many settings, technical support and experiments, I fixed the problem. My code looked like this:

<head runat="server"> <title><asp:ContentPlaceHolder ID="cphPageTitle" runat="server"></asp:ContentPlaceHolder></title> <link href="_private/Normal.css" rel="stylesheet" type="text/css" /> <script src="_private/GoogleAnalytics.js" type="text/javascript" /> </head> 

This caused the problem that I described in my OP. Everything is fixed when I switch to the following:

 <head runat="server"> <title><asp:ContentPlaceHolder ID="cphPageTitle" runat="server"></asp:ContentPlaceHolder></title> <link href="_private/Normal.css" rel="stylesheet" type="text/css" /> <script src="_private/GoogleAnalytics.js" type="text/javascript"></script> </head> 

Note that the script now closes with a full closing tag, rather than a self-closing tag. I suggested that a script may be self-closing without side effects, but everything seems to behave differently when you close it differently. This sheds light on why.

+3
source

As keyboardP has already suggested, you should put the google script analytics in the <body> element, preferably at the end (just before the closing </body> ), so it won’t block the page loading. See http: // developer .yahoo.com / performance / rules.html # js_bottom for justification.

In addition, it’s quite possible that elements (for example, <input> ) have been added to the Google Analytics script that are not allowed inside the <head> element, which explains why the current installation violates your page so much.

+1
source

This is not the correct way to add Google Analytics code. It should look something like this:

 <script type="text/javascript"> var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-XXXXX-X']); _gaq.push(['_trackPageview']); (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> 

... and it should be inserted immediately before the </head> , according to Google:

http://www.google.com/support/analytics/bin/answer.py?hl=en_US&answer=174090&utm_id=ad

+1
source

I use the method of Matthias Binens :

 <script> var _gaq = [['_setAccount', 'UA-XXXXX-X'], ['_trackPageview']]; (function(d, t) { var g = d.createElement(t), s = d.getElementsByTagName(t)[0]; g.async = g.src = '//www.google-analytics.com/ga.js'; s.parentNode.insertBefore(g, s); }(document, 'script')); </script> 

In addition, the only reason Google suggests that you place it as high as possible in the document is the ability to track the visit, which may close from your page until it is fully loaded. I don’t know who honestly would like to track visitors who did not wait for the page to load. This, for me, is not a real visit and discards the numbers. I put it immediately before the closing </body> so that I know that all of my tracked visitors have fully loaded the page.

+1
source

Just

 <script type="text/javascript"> if (!<%=Page.IsPostBack.ToString().Tolower()%>) { var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-xxxxxxxx-xx']); _gaq.push(['_trackPageview']); (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> 
+1
source

All Articles