Wednesday, September 29, 2010

Using Google Analytics in Local Envrionment

If you want to test Google analytics in your local environment first (rather than directly on Production) then please follow below steps.

1. Create Host entry of your local website which should include "http://www..."

e.g.



2. Make host entry in host file(C:\Windows\System32\drivers\etc)
e.g.
127.0.0.1 www.testgoogleanalyticsweb.com

3. Create Profile on Google Analytics with www.testgoogleanalyticsweb.com

For rest of things you can read my previous post

Monday, September 27, 2010

Using Google Analytics in Silverlight Event Tracking

Recently we had a requirement to track event(button click) in Silverlight application using google analytics. I have done research on Google Analytics as well as Microsoft Silverlight Analytics Framework(MSAF). I read that MSAF does not work with Silverlight 3 so I decided to go with Google Analytics. Following is the code we have to use for event tracking.

IN SILVERLIGHT HOST PAGE (e.g.testGA.aspx)
[script type="text/javascript"]
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
[/script]
[script type="text/javascript"]
try {
var pageTracker = _gat._getTracker("YOUR PROFILE ID E.G. UA-XXXXXXX-1");
pageTracker._trackPageview();
}
catch (err) { }

[/script]
[script type="text/javascript"]
function trackEvent(category, action, label) {
pageTracker._trackEvent(category, action, label);
}
[/script]

IN SILVERLIGHT XAML PAGE (e.g. testGA.xaml.vb) ON BUTTON CLICK (if you want to track that how many users have clicked button )

Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click
HtmlPage.Window.Invoke("trackEvent", New Object() {"testGAEventTracking", "AddtoCart", "Click"})
End Sub


Note - Please replace [ with < and ] with > for script block