Given that...

  • I have two variations of a component, Promo and AmazingPromo.
  • I've created a page and set up personalisation to show one or the other based on a condition (for instance if the user is from the UK they see AmazingPromo).
  • I've created a component test to see which one performs better in relation to a goal in Sitecore. (50% UK users see Promo, 50% of UK users see AmazingPromo, and all other users see Promo)

This works great and I can see test data/results in Sitecore.

However, many prefer to use Google for their analytics and I'd like to push events through the GTM datalayer only when a test is running for the user.

So is it possible to do the following:

  • Not from UK: No Event Pushed
  • From UK, Test Renders Promo: Event 'Control' Pushed
  • From UK, Test Renders AmazingPromo: Event 'Variant' Pushed

Currently I've added the push script to each component, but this means that is is called when the test isn't running (i.e. for non UK users) and this distorts the results in GA.

1 Answer 1


This is possible, but youl'll need to modify your component a little.

1: Add a "GTM Event" text field to your datasource item

2: Extend the component's view to something like this:

<div class="promo-component">

    var eventName = Html.Sitecore().Field("GTM Event", new {DisableWebEdit = true}).ToString();

@if (!string.IsNullOrEmpty(eventName))
    @* Push GTM event to dataLayer *@
        window.dataLayer = window.dataLayer || [];
        window.dataLayer.push({ 'event': '@eventName' });

3: Set the respective GTM events field on your personalized datasources or leave empty if none should be triggered.

Update: If you want to only push events while a test is running, check the following in the controller:

Sitecore.Context.Item.Visualization.GetRenderings(Sitecore.Context.Device, true)
            .FirstOrDefault(r => Guid.Parse(r.UniqueId) == RenderingContext.Current.Rendering.UniqueId)?.Settings

Note: I would have expected the property RenderingContext.Current.Rendering.RenderingItem.MultiVariateTest to be set but the value is always empty.

  • This works fine for the personalisation part of the question, the issue is with the component test. Sitecore will render markup based on exactly the same component and datasource for both Not from UK and From UK, Test Renders Promo. The component test is alternating between the default personalisation rule and the defined one. I think the main question is "can I tell whether Sitecore is running a test?". If there was something like if(IsComponentTesting) then I'd be able to wrap code similar to yours in the if to only output the tracking only when needed.
    – SamMullins
    Commented Oct 21, 2019 at 11:35
  • I would have expected that RenderingContext.Current.Rendering.RenderingItem.MultiVariateTest would have the value but it was always empty in my setup. I was able to read the MultiVariateTest property through the context item. See the updated post.
    – Mark Lowe
    Commented Oct 21, 2019 at 14:54
  • In my situation the property PersonalizationTest that was needed rather than MultiVariateTest but it is good to know that both of these exist! Unfortunately from my testing it seems that the value is present whenever the component is rendered. What is needed for this use case is to know if the default variant is being shown because of the test or because the personalisation condition hasn't been met. So using the condition in the question, I only want the PersonalizationTest property to be set if the user is from the UK. For non UK users the test isn't running, so I don't want the value.
    – SamMullins
    Commented Oct 21, 2019 at 15:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.