Posted by : Jebastin Thursday, 7 August 2014

In general we track a page using JavaScript statically. This C# code is used to implement Google Analytics Tracking Script dynamically for every page we want to do it.

Source Code:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Configuration;
  4. using System.Linq;
  5. using System.Web;
  6. namespace KPMGAudit.Web.GoogleAnalytics
  7. {
  8.     public static class Tracking
  9.     {
  10.         public static string TrackingScript(string url)
  11.         {
  12.             var jsTestString = string.Format(@"
  13.                     <script type=""text/javascript"">
  14.                         var _gaq = _gaq || [];
  15.                         _gaq.push(['_setAccount', '{0}']);
  16.                         _gaq.push(['_trackPageview','{1}']);
  17.                         (function() {{
  18.                         var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
  19.                         ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
  20.                         var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  21.                         }})();
  22.                     </script>", ConfigurationManager.AppSettings["GoogleAnalyticsAccountCode"], url);
  23.             return jsTestString;
  24.         }
  25.     }
  26. }
  1. // Inject javascript into Long Description
  2. var url = string.Format("/{0}/{1}", mcContentCategory.AsUrl(), mcTitle.AsUrl());
  3. mcLongDescription = mcLongDescription + GoogleAnalytics.Tracking.TrackingScript(url); 

Another way: Click here.

Leave a Reply

Subscribe to Posts | Subscribe to Comments

Link To This Post/Page

Spread The Word

Add this button to your blog:
JJ Technology Solutions

Blog Archive

Trackers

eXTReMe Tracker
facebook

- Copyright © JJ Technology Solutions - Powered by Source Code Solutions -