Skip to content

Instantly share code, notes, and snippets.

@whyleee
Created November 24, 2016 14:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save whyleee/16932ad245011e0083fbbbf6ccbeea19 to your computer and use it in GitHub Desktop.
Save whyleee/16932ad245011e0083fbbbf6ccbeea19 to your computer and use it in GitHub Desktop.
Fix for Episerver Google Analytics context gadget for multi-hostname websites or dedicated edit server.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using EPiServer.Core;
using EPiServer.GoogleAnalytics.Models;
namespace EPiServer.GoogleAnalytics.Controllers
{
// Fixes Google Analytics context gadget for multi-hostname websites or dedicated edit server.
[Authorize(Roles = "Administrators,GoogleAnalyticsAdministrators,GoogleAnalyticsReaders")]
public class FixedGoogleAnalyticsContextController : EPiServer.GoogleAnalytics.Controllers.Controllers.ControllerBase
{
protected override bool IsGlobal => false;
protected override AnalyticsViewModel GetViewModel(Guid gadgetId)
{
return ViewModelBuilder.GetContextViewModel(gadgetId, User);
}
public override string GetPageUrl(ContentReference contentLink)
{
var url = base.GetPageUrl(contentLink);
var uri = new Uri(url, UriKind.RelativeOrAbsolute);
// FIX: extract hostname filter for absolute urls
if (uri.IsAbsoluteUri)
{
url = uri.PathAndQuery + ";ga:hostname==" + uri.Host;
}
return url;
}
protected override PartialViewResult PartialView(string viewName, object model)
{
// this is required for Episerver to find the views (because this controller is out of module)
viewName = "/EPiServer/EPiServer.GoogleAnalytics/Views/Shared/" + viewName + ".ascx";
return base.PartialView(viewName, model);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using System.Web.Routing;
namespace Website.App_Start
{
public static class Routes
{
public static void Register(RouteCollection routes)
{
// Fix for Google Analytics context gadget
routes.Insert(0, new Route(
"EPiServer/EPiServer.GoogleAnalytics/Context/{action}/{id}",
new RouteValueDictionary(new { controller = "FixedGoogleAnalyticsContext" }),
new RouteValueDictionary(new { action = "GraphData|PageSummaryView|ListsView" }),
new MvcRouteHandler()
));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment