SaaS CMS has officially launched! Learn more now.

Implementing Google Analytics in EPiServer


How do you normally implement GA in EPiServer? What's the most efficient way of doing it?

May 17, 2011 9:06


Do you mean how to inject the GA-script on the pages? I'd guess the most common way to do this would be to modify the masterpage to include the script, but there are numerous other methods, for example to register it programmatically using a pageplugin attached to an appropriate Page-event.



May 17, 2011 9:33


I usually do this from code so that I can check if in edit mode and if so not render the script... Like this:

            // Register GA script
if (!InEditMode)
HtmlGenericControl objControl = new HtmlGenericControl("script");

objControl.Attributes.Add("type", "text/javascript");
objControl.InnerHtml = "var _gaq = _gaq || []; _gaq.push(['_setAccount', 'your_key']); _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') + ''; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); })();";



May 17, 2011 10:10

Thanks guys!

May 18, 2011 3:32

I think the best way is to make a dynamic property called Add2Header, and output that value in the head section of the masterpage.

This way the editor can change the script, or add new ones, without any stress


May 18, 2011 12:36

Very nice suggestion Anders. Looks like a very good approach.

I'll research dynamic property then.

May 19, 2011 0:33
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.