- A marketer wants to add a set of Google Analytics tags to all pages on the site.
- The UX team wants to install heat-map style mouse tracking on the website for a week.
- While Corporate already has Google Analytics installed for the whole site, Bob in Financial Aid wants his own Google Analytics for just his department.
- Marcie wants to try out a new tracking tool, but it requires a script to run in the <html><head/> tag.
This module includes a Page Analytics Scripts Item template that has a Rich Text Editor for the following fields:
- Page Header Script Scripts that need to be rendered in the <head> element
- Body Top Script Scripts that need to be rendered just after the <body> tag
- Body Bottom Script Scripts that need to be rendered just before the </body> tag
There are three Renderings added with this Module, they are predictably:
- Page Header Scripts
- Body Top Scripts
- Body Bottom Scripts
These renderings are each expecting a Datasource Item derived from Page Analytics Scripts. One generally assigns the same Datasource Item to all three Renderings on a given Page. Each Rendering reads from its field, all three renderings together will output the complete script kit required for your instrumentation.
Due to the wide range of information architecture choices in Sitecore, you must specify the Datasource Location for these Renderings. (i.e. Where do you want to store your Analytics Script options.)
Constellation.Feature.PageAnalyticsScripts is managed via NuGet.
In Visual Studio, fire up the Package Manager console and install into a .NET Web Application project:
PM> Install-Package Constellation.Feature.PageAnalyticsScripts
Important: After installing the package, you should build and deploy your solution. Then you must run Sitecore immediately. This will cause the Templates and Rendering Definitions to be installed.
Configure Datasource Locations
After restarting Sitecore, set up your Content Tree location for Page Analytics Script Items and update the Rendering Definitions at:
/Layout/Renderings/Feature/Constellation/Page Analytics Scripts
Create your Scripts Items
If you’re supporting Google Analytics in your site (and who isn’t?) you should create a “Google Scripts” Item in the Datasource Location you configured above. Paste the Google tags into the appropriate fields on the Item.
Repeat this process for any 3rd party scripts you need inserted on page.
Add the Renderings to your Pages
To ensure that every created page gets the Google Analytics scripts, add the Page Heading Scripts, Body Top Scripts, and Body Bottom Scripts renderings to all pages on your site. This can be achieved several different ways:
- You can hard-wire the Renderings to your Layout.
- You can add the Renderings to the Standard Values of your “Page” Templates.
The downside of option 1 is that you will have to hard-wire the datasource ID for your Scripts Item, making it impossible to alter at runtime. A better solution is option 2, which does require that you have (static) Placeholders defined at appropriate points in your HTML to allow for the Renderings to be inserted at runtime.
Having the Renderings inserted via Placeholder ensures that they will be cached appropriately (default cache options are provided). This also ensures that you can easily adjust the datasources of these Renderings at any time without resorting to re-deploying code or HTML.
It’s not uncommon to have multiple analytics vendors involved at the same time, for example:
There are two approaches
- Merge all the scripts into a single Scripts Item which is then rendered on page with the three Renderings as described above.
- Have individual Script Items for each vendor, and have individual Rendering groups for each of these Script Items.
In practice, I’ve seen the first option used far more commonly, because it is easier to update every page of the site by modifying a single Rich Text field, however, if you have analytics kits that don’t appear on every page of your site, you might want to consider more discrete Script items.
Compatibility with “Tag Managers”