/// <summary> /// Renders the script tag that includes the Google Analytics javascript files on the webpage. /// </summary> /// <param name="output">The <see cref="HtmlTextWriter"/> to which the output is rendered.</param> protected static void RenderSnippetStartScript(HtmlTextWriter output) { RenderingUtils.RenderJavaScriptBeginTag(output); output.WriteLine(@"(function() {"); output.WriteLine(@"var ga = document.createElement('script');"); output.WriteLine(@"ga.type = 'text/javascript';"); output.WriteLine(@"ga.async = true;"); output.WriteLine(@"ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';"); output.WriteLine(@"var s = document.getElementsByTagName('script')[0];"); output.WriteLine(@"s.parentNode.insertBefore(ga, s);"); output.WriteLine(@"})();"); //output.WriteLine(@"var gaJsHost = ((""https:"" == document.location.protocol) ? ""https://ssl."" : ""http://www."");"); //output.WriteLine(@"document.write(unescape(""%3Cscript src='"" + gaJsHost + ""google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E""));"); output.RenderEndTag(); }
/// <summary> /// Renders the body of the Google Analytics tracking javascript. /// Updated to Asynchronous Tracking /// </summary> /// <param name="output">Where the script is to be rendered.</param> protected void RenderSnippetBodyScript(HtmlTextWriter output) { RenderingUtils.RenderJavaScriptBeginTag(output); output.WriteLine(@"try {"); // Page Tracker if (this.PageTrackerInUse) { output.WriteLine(string.Format(@"var _gaq=_gaq||[]; _gaq.push(['_setAccount','{0}']);", this.PageTrackerId)); if (!string.IsNullOrEmpty(this.RollupDomainName)) { output.WriteLine(string.Format(@"_gaq.push(['_setDomainName', '{0}']);", this.PageDomainName)); } if (this.AllowPageLinker) { output.WriteLine(@"_gaq.push(['_setAllowLinker', true]);"); } output.WriteLine(@"_gaq.push(['_trackPageview']);"); output.WriteLine(@"_gaq.push(['_trackPageLoadTime']);"); } // RollUp Tracker if (this.RollupTrackerInUse) { output.WriteLine(string.Format(@"var _gaq1=_gaq||[]; _gaq1.push(['_setAccount','{0}']);", this.RollupTrackerId)); if (!string.IsNullOrEmpty(this.RollupDomainName)) { output.WriteLine(string.Format(@"_gaq1.push(['_setDomainName', '{0}']);", this.RollupDomainName)); } if (this.AllowRollUpLinker) { output.WriteLine(@"_gaq1.push(['_setAllowLinker', true]);"); } output.WriteLine(@"_gaq1.push(['_trackPageview']);"); output.WriteLine(@"_gaq1.push(['_trackPageLoadTime']);"); } output.WriteLine(@"} catch(err) { }"); output.RenderEndTag(); }
/// <summary> /// Renders javascript which automatically tracks PDF link clicks and form field changes. /// </summary> /// <param name="output">Where the script is to be rendered.</param> protected void RenderAdditionalTrackingScripts(HtmlTextWriter output) { RenderingUtils.RenderJavaScriptBeginTag(output); output.WriteLine(@"$(document).ready(function(){"); { output.Indent++; // Page Tracker if (this.PageTrackerInUse) { // Cross domain linking bool renderPageDomains = !string.IsNullOrEmpty(this.PageTrackerLinkedDomainsCsv); if (renderPageDomains) { string pageDomainsScriptVarName = string.Format("{0}_pDomains", this.ClientID); output.WriteLine(string.Format( @"var {0} = [{1}];", pageDomainsScriptVarName, this.PageTrackerLinkedDomainsCsv)); output.WriteLine( string.Format( @"jQuery.each({0}, function(index, domain){{ $(""a[href*='""+domain+""']"").click(function(){{ _gaq.push(['_link',this.href]); }}); }});", pageDomainsScriptVarName)); } // PDF Tracking output.WriteLine(@"$(""a[href*='.pdf']"").click(function(){ _gaq.push(['_trackPageview', this.pathname]); }); "); // Form Tracking if (this.EnableFormFieldChangeTracking) { output.WriteLine(@"$(""input"").change(function(){ _gaq.push(['_trackEvent',""Forms"",""Change"",location.pathname+"" - ""+$(this).attr(""name"")]);}); "); output.WriteLine(@"$(""textarea"").change(function(){ _gaq.push(['_trackEvent',""Forms"",""Change"",location.pathname+"" - ""+$(this).attr(""name"")]);}); "); output.WriteLine(@"$(""select"").change(function(){ _gaq.push(['_trackEvent',""Forms"",""Change"",location.pathname+"" - ""+$(this).attr(""name"")]);}); "); } } // Rollup Tracker if (this.RollupTrackerInUse) { // Cross domain linking bool renderRollupDomains = !string.IsNullOrEmpty(this.RollupTrackerLinkedDomainsCsv); if (renderRollupDomains) { string rollupDomainsScriptVarName = string.Format("{0}_rDomains", this.ClientID); output.WriteLine(string.Format( @"var {0} = [{1}];", rollupDomainsScriptVarName, this.RollupTrackerLinkedDomainsCsv)); output.WriteLine(string.Format( @"jQuery.each({0}, function(index,domain){{ $(""a[href*='""+domain+""']"").click(function(){{ _gaq1.push(['_link',this.href]);}});}});", rollupDomainsScriptVarName)); } // PDF Tracking output.WriteLine(@"$(""a[href*='.pdf']"").click(function(){ _gaq1.push(['_trackPageview', this.pathname]); }); "); // Form Tracking if (this.EnableFormFieldChangeTracking) { output.WriteLine(@"$(""input"").change(function(){ _gaq1.push(['_trackEvent',""Forms"",""Change"",location.pathname+"" - ""+$(this).attr(""name"")]); }); "); output.WriteLine(@"$(""textarea"").change(function(){ _gaq1.push(['_trackEvent',""Forms"",""Change"",location.pathname+"" - ""+$(this).attr(""name"")]); }); "); output.WriteLine(@"$(""select"").change(function(){ _gaq1.push(['_trackEvent',""Forms"",""Change"",location.pathname+"" - ""+$(this).attr(""name"")]); }); "); } } output.Indent--; } output.WriteLine(" });"); output.RenderEndTag(); }