We help IT Professionals succeed at work.

Google Analytics in asp.net

KaranGupta
KaranGupta asked
on
Hi

I want to impement google analytics in my asp.net application.

So I referred this tutorial
http://www.aspcode.net/The-Google-Analytics-ASPNET-webcontrol.aspx

Then I have created a custom control and add following code in it.(please see the code section)

Then I have referred the dll in my project and added the following code in my .aspx file

<%@ Register TagPrefix="analytics" TagName="GoogleAnalytics" Namespace="GoogleAnalyticsCtrl" Assembly="GoogleAnalyticsCtrl"%>

<analytics:GoogleAnalytics id="google1"  runat="server" uacct="UA-zzzzz-x"  DebugMode="false"></analytics:GoogleAnalytics>

But I am getting this error


1. The directive is missing a 'src' attribute.      
2.Unknown server tag 'analytics:GoogleAnalytics'.      


Is there anything wrong with the code.

Kindly advice
Regards
Karan      

Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Text
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Namespace GoogleAnalyticsCtrl
    <DefaultProperty("uacct")> _
    <ToolboxData("<{0}:GoogleAnalytics runat=server></{0}:GoogleAnalytics>")> _
    Public Class GoogleAnalytics
        Inherits WebControl

        Protected Overrides Sub OnInit(ByVal e As EventArgs)
            Me.EnableViewState = False
            MyBase.OnInit(e)

        End Sub
        Private m_uacct As String = "UA-"
        <Bindable(True)> _
        <Category("Appearance")> _
        <DefaultValue("")> _
        <Localizable(True)> _
        Public Property uacct() As String
            Get
                Return m_uacct
            End Get

            Set(ByVal value As String)
                m_uacct = value

            End Set

        End Property

        Private m_DebugMode As [Boolean] = False

        <Bindable(True)> _
         <Category("Appearance")> _
         <DefaultValue("true")> _
         <Localizable(True)> _
         Public Property DebugMode() As [Boolean]

            Get

                Return m_DebugMode

            End Get



            Set(ByVal value As [Boolean])

                m_DebugMode = value

            End Set

        End Property





        Protected Overrides Sub RenderContents(ByVal output As HtmlTextWriter)

            If DebugMode Then

                Return

            End If

            Dim sScript As String = "<script src=""http://www.google-analytics.com/urchin.js"" type=""text/javascript"">" & vbCr & vbLf & "</script>" & vbCr & vbLf & "<script type=""text/javascript"">" & vbCr & vbLf & "_uacct = """ & uacct & """;" & vbCr & vbLf & "urchinTracker();" & vbCr & vbLf & "</script>"

            output.Write(sScript)
        End Sub

    End Class

End Namespace

Open in new window

Comment
Watch Question

Commented:
Hey

 'analytics:GoogleAnalytics

The dll is not referenced or not being called. Try removing the reference and adding again.

Btw why do you need all this why not just add the javascript to the master page. I first thought you have some custom requirement

But

 Dim sScript As String = "<script src=""http://www.google-analytics.com/urchin.js"" type=""text/javascript"">" & vbCr & vbLf & "</script>" & vbCr & vbLf & "<script type=""text/javascript"">" & vbCr & vbLf & "_uacct = """ & uacct & """;" & vbCr & vbLf & "urchinTracker();" & vbCr & vbLf & "</script>"


So why not just this in the master page.

Author

Commented:
Hi

Is there any function where I should put this code.

Secondly after I add the code I want to show the information on my web page. How can I do that

Commented:
Hey,

Analytics is a simple javascript client side code. So just copy the js code from your analytics and paste it before the page </body>

and

Secondly after I add the code I want to show the information on my web page. How can I do that

How do you mean ?

Author

Commented:
Hi

I have pasted the code before the body tag.

What is this code meant for.

Can this code give data about no of visitors etc.

Commented:
Yes google analytics tracks all your visitors which you view all the visitor info  in the google analytic pagecontrol panel. page