0

I am using Sitecore 9.1 with SXA 1.8.1. I am trying to trigger programmatically a page event via API (the page exists in sitecore however FE for it is built in vue.js) so on FE there are some events data attached to buttons in this app and when the user clicks on it, the information is being passed to the backend API responsible to register the event.

When I debug the code, the page event seems to be registered correctly, all data are passed to RegisterPageEvent method and no exception happens on the way. However the events are not registered as they were supposed to be and when I try to preview the page events in the path analyzer I cannot see any of them.

What could be the issue and how can I investigate this issue further?

API Controller code:

[HttpPost]
    public void RegisterAnalyticsEvent(EventModel model)
    {
        try
        {
            this._eventsTrackingService.StartTrackerIfNotYetActive();

            if (!this._eventModelBuilder.IsValid(model))
            {
                return;
            }

            model = this._eventModelBuilder.Build(model.Action, model.Category, model.EventName, model.Label);

            this._eventsTrackingService.RegisterEvent(model);
        }
        catch (Exception exception)
        {
            var message = $"Unable to register page event for sitecoreEventName='{model.EventName}', category='{model.Category}', action='{model.Action}', label='{model.Label}.";
            this._logger.LogError($"An error occurred in {GetType().Name} {MethodBase.GetCurrentMethod().Name}. {message}", exception, this);
        }
    }

EventsTrackingService:

public void StartTrackerIfNotYetActive()
    {
        if (!Tracker.IsActive)
        {
            Tracker.StartTracking();
        }
    }

    public void RegisterEvent(EventModel model)
    {
        var eventDefinitionId = FindGuidForEventDefinition(model.Category, model.EventName);
        _logger.LogInfo($"Retrieved everntDefinitionId = {eventDefinitionId}", this);

        if (model.Category.Equals(Constants.PageEvent, StringComparison.InvariantCultureIgnoreCase))
        {
            var pageData = RegisterPageEvent(eventDefinitionId);
            RegisterPageData(model, pageData);
        }
    }

    private PageEventData RegisterPageEvent(Guid eventDefinitionId)
    {
        var pageEventDefinition = Tracker.MarketingDefinitions.PageEvents[eventDefinitionId];

        if (pageEventDefinition == null)
        {
            _logger.LogError($"Unable to retrieve page event definition from event ID {eventDefinitionId}", this);

            return null;
        }

        Tracker.Current.CurrentPage.RegisterPageEvent(pageEventDefinition);

        _logger.LogInfo($"Registered page event {pageEventDefinition.Alias}", this);

        return new PageEventData(pageEventDefinition.Alias, pageEventDefinition.Id);
    }

    private void RegisterPageData(EventModel model, PageEventData pageData)
    {
        try
        {
            if (pageData == null)
            {
                _logger.LogError($"Unable to register Page Data for event={model.EventName}", this);
                return;
            }

            pageData.Data = model.Action ?? string.Empty;
            pageData.DataKey = model.Label ?? string.Empty;
            pageData.Text = model.Action ?? string.Empty;

            Tracker.Current.CurrentPage.Register(pageData);
        }
        catch (Exception exception)
        {
            this._logger.LogError($"RegisterPageData, Page event {model.EventName} couldn't be registered on {Tracker.Current.CurrentPage.Url.Path}.", exception, this);
            throw;
        }
    }

    private Guid FindGuidForEventDefinition(string category, string sitecoreEventName)
    {
        ISearchIndex index = ContentSearchManager.GetIndex("sitecore_master_index");

        using (IProviderSearchContext context = index.CreateSearchContext())
        {
            switch (category.ToLower())
            {
                case Constants.PageEvent:
                    return GetSitecoreEventItemId(category, sitecoreEventName, context);
                default:
                    this._logger.LogError($"Unable to get event definition ID for sitecoreEventName='{sitecoreEventName}', category='{category}'.", this);
                    return Guid.Empty;
            }
        }
    }

    private Guid GetSitecoreEventItemId(string category, string sitecoreEventName, IProviderSearchContext context)
    {
        var path = CategoryPaths[category];

        return context.GetQueryable<SearchResultItem>()
            .Where(x => x.Path.StartsWith(path) && x.Name.Equals(sitecoreEventName, StringComparison.InvariantCultureIgnoreCase))
            .FirstOrDefault()
            ?.ItemId
            ?.Guid ?? Guid.Empty;
    }
3
  • When you are triggering your client side API calls, are you including cookie and security? If you make the call without security, then Sitecore treats it as a new session and puts your event on a different contact/session. – Chris Auer Apr 22 at 15:39
  • I am actually not identifying contact at all, I just wanted to see that particular button was clicked no matter who did that. – 9_9 Apr 23 at 7:10
  • 1
    Do you have anonymous contact indexes on in xConnect? – Chris Auer Apr 23 at 15:51

Your Answer

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

Browse other questions tagged or ask your own question.