Implementing Google Analytics in EPiServer

Vote:
 

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

#50910
May 17, 2011 9:06
Vote:
 

Hi!

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.

Regards,
Johan

 

#50916
May 17, 2011 9:33
Vote:
 

Hi

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') + '.google-analytics.com/ga.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); })();";

Page.Header.Controls.Add(objControl);
}

 

#50917
May 17, 2011 10:10
Vote:
 

Thanks guys!

#50946
May 18, 2011 3:32
Vote:
 

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

 

#50976
May 18, 2011 12:36
Vote:
 

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

I'll research dynamic property then.

#50994
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.