andrew4582

Analytics

Jan 16th, 2011
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.68 KB | None | 0 0
  1. namespace Microsoft.Web.Helpers
  2. {
  3.     using System;
  4.     using System.Globalization;
  5.     using System.Web;
  6.  
  7.     public static class Analytics
  8.     {
  9.         public static HtmlString GetGoogleAsyncHtml(string webPropertyId)
  10.         {
  11.             bool addDoubleQuotes = false;
  12.             webPropertyId = HttpUtility.JavaScriptStringEncode(webPropertyId, addDoubleQuotes);
  13.             return new HtmlString(string.Format(CultureInfo.InvariantCulture, "<script type=\"text/javascript\">\nvar _gaq = _gaq || [];\n_gaq.push(['_setAccount', '{0}']);\n_gaq.push(['_trackPageview']);\n(function() {{\nvar ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;\nga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';\nvar s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);\n}})();\n</script>\n", new object[] { webPropertyId }));
  14.         }
  15.  
  16.         public static HtmlString GetGoogleHtml(string webPropertyId)
  17.         {
  18.             bool addDoubleQuotes = true;
  19.             webPropertyId = HttpUtility.JavaScriptStringEncode(webPropertyId, addDoubleQuotes);
  20.             return new HtmlString(string.Format(CultureInfo.InvariantCulture, "<script type=\"text/javascript\">\nvar gaJsHost = ((\"https:\" == document.location.protocol) ? \"https://ssl.\" : \"http://www.\");\ndocument.write(unescape(\"%3Cscript src='\" + gaJsHost + \"google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E\"));\n</script>\n<script type=\"text/javascript\">\ntry{{\nvar pageTracker = _gat._getTracker({0});\npageTracker._trackPageview();\n}} catch(err) {{}}\n</script>\n", new object[] { webPropertyId }));
  21.         }
  22.  
  23.         public static HtmlString GetStatCounterHtml(int project, string security)
  24.         {
  25.             string str = HttpUtility.HtmlAttributeEncode(string.Format(CultureInfo.InvariantCulture, "https://c.statcounter.com/{0}/0/{1}/1/", new object[] { project, HttpUtility.UrlPathEncode(security) }));
  26.             bool addDoubleQuotes = true;
  27.             security = HttpUtility.JavaScriptStringEncode(security, addDoubleQuotes);
  28.             return new HtmlString(string.Format(CultureInfo.InvariantCulture, "<!-- Start of StatCounter Code -->\n<script type=\"text/javascript\">\nvar sc_project={0};\nvar sc_invisible=1;\nvar sc_security={1};\nvar sc_text=2;\nvar sc_https=1;\nvar scJsHost = ((\"https:\" == document.location.protocol) ? \"https://secure.\" : \"http://www.\");\ndocument.write(\"<sc\" + \"ript type='text/javascript' src='\" + scJsHost + \"statcounter.com/counter/counter_xhtml.js'></\" + \"script>\");\n</script>\n\n<noscript><div class=\"statcounter\"><a title=\"tumblrstatistics\" class=\"statcounter\" href=\"http://www.statcounter.com/tumblr/\"><img class=\"statcounter\" src=\"{2}\" alt=\"tumblr statistics\"/></a></div></noscript>\n<!-- End of StatCounter Code -->", new object[] { project, security, str }));
  29.         }
  30.  
  31.         public static HtmlString GetYahooHtml(string account)
  32.         {
  33.             bool addDoubleQuotes = true;
  34.             account = HttpUtility.JavaScriptStringEncode(account, addDoubleQuotes);
  35.             return new HtmlString(string.Format(CultureInfo.InvariantCulture, "<SCRIPT language=\"JavaScript\" type=\"text/javascript\">\n<!-- Yahoo! Inc.\nwindow.ysm_customData = new Object();\nwindow.ysm_customData.conversion = \"transId=,currency=,amount=\";\nvar ysm_accountid = {0};\ndocument.write(\"<SCR\" + \"IPT language='JavaScript' type='text/javascript' \"\n+ \"SRC=//\" + \"srv3.wa.marketingsolutions.yahoo.com\" + \"/script/ScriptServlet\" + \"?aid=\" + ysm_accountid\n+ \"></SCR\" + \"IPT>\");\n// -->\n</SCRIPT>\n", new object[] { account }));
  36.         }
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment