1

We have requirement where we do need get Clickstream report for various Sitecore pages where each and every single click needs to tracked in Analytics system like Page,component and link clicked needs to be tracked

Can some body suggest possible solutions

3

I had a project where we had to track all the clicks and views of the user. The implementation was to use the Sitecore Page Events.

Assuming you are using Sitecore 7.5 to 8.

Summary

  1. Create a category for the content click event in Sitecore.
  2. Add a checkbox whether a content should have its click event tracked.
  3. Implementation of a method to register the Page Event in MongoDB.
  4. Implementation of an aggregation processor to take data from MongoDB to SQL Reporting Database.

Sitecore Items

Create a Page Event Category under the path /sitecore/system/Settings/Analytics/Page Events. Example, you can named it Content Events Click.

Since we had a requirement whereby not all contents should be tracked, I have created a checkbox named Is Tracked on the content item template. Then, I have implemented a method to create a Page Event Item under the path /sitecore/system/Settings/Analytics/Page Events/Content Events Click. The method is triggered on item saved. I will only create the Page Event Item if the checkbox Is Tracked is checked.

Code Snippet of the ItemSavedEventHandler

private readonly ID clickPageEventCategoryItemId = new ID("Item ID of the Content Event Click");

public void OnItemSaved(object sender, EventArgs args)
{
    var savedItem = Event.ExtractParameter(args, 0) as Item;

    if (savedItem == null || savedItem.Database.Name.ToLower() != "master")
    {
        return;
    }

    var itemId = savedItem.ID.ToGuid().ToString();

    var clickEventName = $"{itemId}_click";

    var clickEventItem = savedItem.Database.SelectSingleItem($"/sitecore/system/Settings/Analytics/Page Events/Content Events Click/*[@@Name = '{itemId}_click']");

    if (clickEventItem != null)
    {
        return;
    }

    savedItem.Fields.ReadAll();

    Sitecore.Data.Fields.CheckboxField trackPerformanceField = savedItem.Fields["Is Tracked"];

    if (!trackPerformanceField.Checked)
    {
        return;
    }

    var clickPageEventCategoryItem = savedItem.Database.GetItem(this.clickPageEventCategoryItemId);

    using (new EventDisabler())
    {
        using (new SecurityDisabler())
        {
            clickEventItem = clickPageEventCategoryItem.Add(clickEventName, new TemplateID(PageEventItem.TemplateID));

            var clickPageEventItem = PageEventItem.Create(clickEventItem);
            clickPageEventItem.BeginEdit();
            clickPageEventItem.IsSystem = true;
            clickPageEventItem.EndEdit();

            using (new EditContext(clickEventItem))
            {
                clickEventItem.Fields[FieldIDs.WorkflowState].Value = "EDCBB550-BED3-490F-82B8-7B2F14CCD26E";
                clickEventItem.Fields[FieldIDs.DisplayName].Value = clickEventName;
                clickEventItem.Fields[PageEventItem.FieldIDs.Points].Value = "0";
            }
        }
    }
}

Screenshot

enter image description here

Then you need to have a method which is called each time the component or page is being clicked.

Code snippet to register the click event

public void CreateClickContentEvent(Guid itemId)
{
    if (Tracker.IsActive && Tracker.Current != null)
    {
        Assert.IsNotNull(Tracker.Current, "Tracker.Current");

        if (Tracker.Current != null)
        {
            Assert.IsNotNull(Tracker.Current.Session, "Tracker.Current.Session");

            CurrentInteraction interaction = Tracker.Current.Session.Interaction;

            Assert.IsNotNull(interaction, "Tracker.Current.Session.Interaction");
            Assert.IsNotNull(interaction.CurrentPage, "Tracker.Current.Session.Interaction.CurrentPage");
        }

        var clickEventName = $"{itemId}_click";

        var itemPath = $"/sitecore/system/Settings/Analytics/Page Events/Content Events Click/{clickEventName}";

        var clickEventItem = Context.Database.GetItem(itemPath, Language.Parse("en"));

        if (clickEventItem != null)
        {
            PageEventData pageEventData = new PageEventData(clickEventName, clickEventItem.ID.ToGuid())
            {
                ItemId = itemId,
                Data = itemId.ToString(),
                DataKey = itemId.ToString("B"),
                PageEventDefinitionId = clickEventItem.ID.ToGuid(),
                Text = "Clicked",
                Name = clickEventName
            };

            if (Tracker.Current.Contact != null)
            {
                try
                {
                    Tracker.Current.Interaction.CurrentPage.Register(pageEventData);
                }
                catch (Exception)
                {
                    Log.Error($"Error on register tracking... ", "CreatePageEventWithGoal");
                }
            }
        }
    }
}

From the above code, you will see that I am checking if the clicked item has its id in the Content Event Click container. If it is not present, the click will not be registered in MongoDB.

Once the data has been captured in MongoDB, you will need to implement an aggregator which will aggregates the data from MongoDB to the Sitecore Reporting Database. You can read more from my blog post on How to setup the Sitecore Aggregation.

Note:

  • You will need to have a custom table to add those data

  • Implement a dashboard or page on the Sitecore Experience Analytics Page to read the data.

  • You may need to change the code or flow based on your requirements.

Reference to my article is here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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