- Back to Home »
- ASP.Net , C# , Javascript »
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.
Another way: Click here.
Source Code:
- using System;
- using System.Collections.Generic;
- using System.Configuration;
- using System.Linq;
- using System.Web;
- namespace KPMGAudit.Web.GoogleAnalytics
- {
- public static class Tracking
- {
- public static string TrackingScript(string url)
- {
- var jsTestString = string.Format(@"
- <script type=""text/javascript"">
- var _gaq = _gaq || [];
- _gaq.push(['_setAccount', '{0}']);
- _gaq.push(['_trackPageview','{1}']);
- (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>", ConfigurationManager.AppSettings["GoogleAnalyticsAccountCode"], url);
- return jsTestString;
- }
- }
- }
- // Inject javascript into Long Description
- var url = string.Format("/{0}/{1}", mcContentCategory.AsUrl(), mcTitle.AsUrl());
- mcLongDescription = mcLongDescription + GoogleAnalytics.Tracking.TrackingScript(url);
Another way: Click here.