Posts Tagged ‘google analytics’

Google Analytics için Asp.NET WebControl

Kasım 25th, 2009

Özelliğini WebSitenize ekleyebilmeniz için kolaylık sağlayacak bir WebControl.

Bu WebControl ile 2 Analytics Mod’unu da kullanabilirsiniz;

  • Analytics GA
  • Analytics Urchin

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
}

Kullanılışı:


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Test.aspx.cs" Inherits="WebTest.WebForm.Forms.Test" %>

<%@ Register assembly="WebTest.WebForm.Forms" namespace="WebTest.WebForm.Forms" tagprefix="cc1" %>

<!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>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <cc1:GoogleAnalytics ID="GoogleAnalytics1" runat="server">
        </cc1:GoogleAnalytics>

    </div>
    </form>
</body>
</html>