• Code
  • Tags
  • Users
  • Titles
  • Log in
  • Feedback
  • FAQ
Share Code
Welcome to ForkCan.com

ForkCan is all about sharing code in a social way.

Discuss, debate or argue with other devs about their or your own code.

Give other devs feedback or make a Fork (Make a better version of a shared code).

Rate the code, if you use the code mark it as used so others can see if the shared code is used by someone.

Help each other to be better devs and to be more productive.


Features not working yet:

Flag a post


QR Code

Tiny Url

http://4kcan.com/s/MjIy

Related Code
Make sure the web.config pages/namespace can be used together with ASP.NET MVC 3 P1 and Razor
StringFormat Extension method for HtmlHelper ASP.NET MVC
Using DynamicObject to get a value from the Request.Params
PagedList class and paging code for ASP.Net MVC (Nothing new but kind of usefull.)
Unity Service Locator for ASP.NET MVC 3.0 Beta 1
Fork of GZIP your Actions in MVC if not used as standard in IIS7 or only use IIS6
Unity Controller Factory for ASP.NET MVC 3.0
UnitOfWork Action filter for ASP.NET MVC and nHibernate
ASP.NET MVC 4.0 Web API Message logger for .Net 4.0
GZIP your Actions in MVC if not used as standard in IIS7 or only use IIS6
Extension to the HttpClient

Google Analytics helper for ASP.NET Razor


1
1.89k 0 0 0 0 0

 public static class Google
 {
    public static IHtmlString GetAnalyticsHtml(string id = null)
    {
        var accountId = id;

        if (string.IsNullOrWhiteSpace(accountId))
            accountId = ConfigurationManager.AppSettings["GoogleAnalyticAccount"];

        if (string.IsNullOrWhiteSpace(accountId))
            throw new ApplicationException("You must specify a Google Account Id, you can for example use appSettings with the key 'GoogleAnalyticAccount' in web.config or pass the id as an argument");

        return
            new HtmlString(@"
                            <script type=""text/javascript"">
                            var _gaq = _gaq || [];
                            _gaq.push(['_setAccount', '" + accountId + @"']);
                            _gaq.push(['_trackPageview']);

                            (function () {
                               var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
                               ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
                               var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
                            })();
                            </script>");
    }
}

Just add the following to a page using Razor:

@Google.GetAnalyticsHtml()

WebMatrix have several helper methods for this, but they aren't part of the ASP.Net MVC 3.0 and the Razor view engine, so I created one.


Share: twitter | facebook   Action: used | fork | flag

aspmvc

Mark 'aspmvc' tag as 'like'

Mark 'aspmvc' tag as 'ignore'

c#

Mark 'c#' tag as 'like'

Mark 'c#' tag as 'ignore'

razor

Mark 'razor' tag as 'like'

Mark 'razor' tag as 'ignore'

razor-helpers

Mark 'razor-helpers' tag as 'like'

Mark 'razor-helpers' tag as 'ignore'


 @fredrikn "I'm the master"
3.11k
August 01, 2010 9:50 AM
edited August 02, 2010 3:54 PM

Fork

 Google Analytics helper for ASP.NET Razor -  @fredrikn Sunday 01, 2010 9:50 AM


0 Feedback


You must log in before you can give any feedback


0 Discussion(s)

Newest Oldest

You must log in before you can post a comment

Squeed
Made by: Fredrik Normén 2010