using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web.UI.WebControls.WebParts; using Microsoft.SharePoint; using System.Web.Caching; namespace WebParts //TODO: fix { /// /// Inserts the google analysis script on the page. /// The control ensures that the js file is also included on the page, i.e. don't /// explicitly include it on the master page. /// /// Note that there is only one UA code used - it is assumed that the key will remain the /// same across all environments and will be separated in the GA by filters. /// public class UsageAnalysis : WebPart { string uaKey = "" // "UA-XXXXXXX-1"; public string UAKey { get { return uaKey; } set { uaKey = value; } } protected override void OnLoad(EventArgs e) { string injectionString = string.Empty; if (this.Page.Cache[SPContext.Current.Web.CurrentUser.LoginName] == null) { SPUser user = SPContext.Current.Web.CurrentUser; injectionString = ";" + user.LoginName.Substring(0, user.LoginName.IndexOf("\\")); foreach (SPGroup group in user.Groups) { injectionString += ";" + group.Name; } injectionString += ";"; this.Page.Cache[SPContext.Current.Web.CurrentUser.LoginName] = injectionString; } string GA_TRACKING_SCRIPT = @" "; Page.ClientScript.RegisterStartupScript(this.GetType(), "GoogleAnalyticsTracking", string.Format(GA_TRACKING_SCRIPT, UAKey)); } } }