MVC
This section describes how to add tracking and personalization to a site built using ASP.NET MVC.
#
Add tracker script to SitecoreCopy the file
uniform.optimize.min.js
to theScripts
folder on your Sitecore instance.note
If you want the script to be loaded from a different location, there are several options available. See rendering modes for more information.
note
For Uniform for Sitecore solutions with Deploy and Optimize, the file must be copied to
public/scripts
folder inUniform Starter Kit
.
#
Add tracker to layout viewIn a text editor, open the view that represents the site layout.
Add the following to the top of the view:
@using Uniform.Optimize.Mvc@using Uniform.Optimize.Tracking
Add the following to the content of the
<head>
tag:@Html.Uniform().Context()
note
This code results in Edge Context being injected into the page in a script tag. It is critical that Edge Context be position on the page before any personalization. Edge Context consists of instructions that are executed before the page is returned to the browser and because those instructions generate no output, there is no risk that this code will result in invalid markup.
Add the following inside the
<body>
tag:@Html.Uniform().Tracker()
note
This code results in JavaScript that loads the Uniform tracker after the page is loaded so that page rendering is not blocked.
Save the view and deploy it to your site.
#
Handling global renderingstip
If your site does not include personalized global renderings
that are added dynamically (e.g. using the pipeline
mvc.buildPageDefinition
), skip this section.
In order for Uniform personalization to work, Uniform must resolve all personalization rules that are assigned to the renderings on a page. By default, Uniform only recognizes the renderings that are assigned to the context item's presentation details.
If you add renderings to a page programatically, Uniform will not recognize those renderings. This is only a problem if those renderings are personalized. In this case, you must enable a setting that forces Uniform to use the renderings on the page definition instead of the renderings assigned to the presentation details.
Deploy the following Sitecore config patch file to your Sitecore instances:
<getPersonalizedRenderings> <processor type="Uniform.Pipelines.GetPersonalizedRenderings.GetRenderingsFromItem, Uniform.Content.Sitecore"> <usePageDefinition>true</usePageDefinition> </processor></getPersonalizedRenderings>
Another option is to extend the pipeline getPersonalizedRenderings
.
More information on this pipeline is available in the
reference section.
#
TroubleshootingYou can use the following steps to confirm the tracker is loaded on your site:
Open your browser's developer tools.
Navigate to your site.
In the JavaScript console, the following code should display the version of Uniform Optimize you installed:
window.uniform?.tracker?.version
If the tracker version is reported as undefined
, this means the
tracker was not loaded. This indicates one of the following may
be true:
#
Tracker script is not being loadedIn your browser's developer tools, confirm a request is being sent
for the tracker script uniform.optimize.min.js
. Make sure that a
response is returned.
If a 404 error is returned, confirm the tracker script is deployed to the correct path on your Sitecore CD instance.
#
JavaScript is disabled on your browserJavaScript is used to load the tracker script. If JavaScript is disabled, the tracker script cannot be loaded.