web 2.0

Google Analytics WebControl for ASP.NET

Hi,

Tonight I’ll provide a Quick Useful WebControl in order to add Google Analytics Feature to your ASP.NET WebSite.

This WebControl allow you to choose between the 2 different Analytics Mode :

  • Analytics GA
  • Analytics Urchin

Here is the Code :

[DefaultProperty("Text")]
[ToolboxData("<{0}:GoogleAnalytics runat=server></{0}:GoogleAnalytics>")]
public class GoogleAnalytics : Control
{
    #region Inner Enum

    public enum AnalyticMode
    {
        Urchin,
        GA
    }

    #endregion

    #region Public Properties

    public String UACode
    {
        get { return ViewState["UACode"] as String; }
        set { ViewState["UACode"] = value; }
    }
    public AnalyticMode GMode
    {
        get { return (AnalyticMode)ViewState["Mode"]; }
        set { ViewState["Mode"] = value; }
    }

    #endregion

    #region Protected Overrides Methods

    protected override void Render(HtmlTextWriter output)
    {
        switch (GMode)
        {
            case AnalyticMode.GA:
                output.Write(GetGAString());
                break;
            case AnalyticMode.Urchin:
                output.Write(GetUrchinString());
                break;
            default:
                output.Write(GetGAString());
                break;
        }
    }

    #endregion

    #region Private Methods

    private String GetUrchinString()
    {
        StringBuilder sb = new StringBuilder();
        sb.AppendLine("<script src=\"http://www.google-analytics.com/urchin.js\" type=\"text/javascript\">");
        sb.AppendLine("</script>");
        sb.AppendLine("<script type=\"text/javascript\">");
        sb.AppendLine("_uacct = \"" + UACode + "\";");
        sb.AppendLine("urchinTracker();");
        sb.AppendLine("</script>");
        return sb.ToString();
    }

    private String GetGAString()
    {
        StringBuilder sb = new StringBuilder();
        sb.AppendLine("<script type=\"text/javascript\">");
        sb.AppendLine("var gaJsHost = ((\"https:\" == document.location.protocol) ? \"https://ssl.\" : \"http://www.\");");
        sb.AppendLine("document.write(unescape(\"%3Cscript src='\" + gaJsHost + \"google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E\"));");
        sb.AppendLine("</script>");
        sb.AppendLine("<script type=\"text/javascript\">");
        sb.AppendLine("var pageTracker = _gat._getTracker(\"" + UACode + "\");");
        sb.AppendLine("pageTracker._initData();");
        sb.AppendLine("pageTracker._trackPageview();");
        sb.AppendLine("</script>");
        return sb.ToString();
    }

    #endregion
}

Sample Usage :

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Sb2.Web.Tests._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <Sb2:GoogleAnalytics ID="analytics" runat="server" GMode="GA" UACode="XXXXX" />
    </div>
    </form>
</body>
</html>

 

Note : I advice to put this WebControl at the Bottom of your Page / MasterPage for Performance Issue.

Hope this help’s!



Views(2084)

kick it on DotNetKicks.com

Share/Save/Bookmark Subscribe

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: ,

ASP.NET

Comments

Technorati Profile