views:

239

answers:

3

I know that I need to add the tracking code snippet at the bottom of all my pages, but is there a central location to do this?

Or do I need to add this tracking code to all of my templates?

I guess that I could wrap the snippet in a user control, or external .js file, and reference it on each page, but is there a global footer somewhere? The site I'm working on has about 30-40 layouts, and adding it to each one would be a pain!

Thanks in advance!

+5  A: 

Actually, the role of a Sitecore layout is exactly this; to act as a global file that all individual page templates "derive" from.

Normally you'd stick the analytics code into the master layout, and use Sitecore sublayout/placeholder techniques to construct the various page templates you need. You would not normally need more than perhaps one or two layouts for any device you are serving content to. And I guess for most sites, the only device in use is regular web content delivery.

That being said, what you could do, is have all the layouts inherit their codebase from a common base class (inheriting from Page), and inject the google code centrally from here. Would still require you to go through all layout files however.

Mark Cassidy
What Mark said. Also, you might be interested in the Sitecore Google Analytics module (available here: http://trac.sitecore.net/SitecoreGoogleAnalytics) which allows you to specify the tracking code within the Sitecore client and access to GA Reporting.
Tchami
+1  A: 

I have not tried the module, I think that is codebehind version. I have made this in XSLT, its pretty fast and easy to make. I have footer.xslt where I put the code that simply checks if page you are standing on uses template that I want to index and does not belong to page names that I want to exclude. Then I have an item with a custom template for Google Analytics with following memo fields. IncludeTemplates -field contains list of templates that I want to include for analytics : ExcludeItemsNames -field for excluding pages by item name

contains($includeTemplates, concat('|',./@template,'|')) and not(contains($excludeItemNames, concat('|',./@template,'|')))

Remember @key and @template is always in small letters

If you run many domains don't forget to add pageTracker._setDomainName("www.example.com"); in analytics script so you can separate sub-domains etc. if they use same footer.xslt

jpkeisala
+1  A: 

Normally we consider the actual Google code as content. Within Sitecore we normally have a settings folder, something like /sitecore/content/settings. This exists outside the root of the site. Beneath this have a settings item with a plain multi-line text field, I think the field type is memo or something similar.

Afterwards create an XSLT that renders out the content of this settings item. Something like (assuming the field is called value in the setting item):

<xsl:value-of select="sc:fld('Value','/sitecore/content/settings/footerJavaScript')" />

You may or may not need to set the disable-output-escaping attribute.

Then on the aspx page that your pages use as the template add a control that looks at the xslt rendering:

<sc:XslFile runat="server" Path="/xsl/footerJavaScript" />

The reason that we normally keep the javascript as content is this allows the client to change the analytics code without having to contact us.

Michael Edwards