4

I need to trigger goals dynamically in Sitecore from an independent web portal. The user clicks on a button and I make a rest api call to my sitecore service

var catId= 'clothing';

var url = 'https://hostname/sitecore/api/ssc/itemaccess/' + catId

I get the error: Tracker.Current is not initialized.

What am I doing wrong? Can I even do it this way?

My controller code is:

[Route("sitecore/api/ssc/itemaccess/{catId}")]
public class TriggerGoalController : ApiController
{
    [HttpGet]
    public string Get(string catId)
    {
        try
        {

            if (!Sitecore.Analytics.Tracker.IsActive)
            {
                Sitecore.Analytics.Tracker.StartTracking();
            }

            if (Sitecore.Analytics.Tracker.IsActive && Sitecore.Analytics.Tracker.Current.CurrentPage != null)
            {



                var goalItem = //getGoalItem logic;

                if (goalItem != null)
                {

                    var goalTrigger = Sitecore.Analytics.Tracker.MarketingDefinitions.Goals[goalItem.ID.ToGuid()];
                    var goalEventData = Sitecore.Analytics.Tracker.Current.CurrentPage.RegisterGoal(goalTrigger);
                    goalEventData.Data = goalItem[goalTrigger.Name];
                    goalEventData.ItemId = goalItem.ID.ToGuid();
                    goalEventData.DataKey = goalItem.Paths.Path;
                                            Sitecore.Analytics.Tracker.Current.Interaction.AcceptModifications();
                    return goalTrigger.Name;
                }
            }
            return "Something went wrong";
        }
        catch (Exception ex)
        {
            return ex.Message;
        }
    }
}
5

Tracker.Current relies on HTTP Session Context existing. Web API does not instantiate a Session object, thereby making Tracker completely unusable.

You can only use Tracker in the context of a Web Page / Controller-View Rendering. For a walkthrough on what the Tracker is doing and a full explanation on how to troubleshoot these types of issues, you can visit this blog post: Analytics Tracker Common Issues and How to Resolve Them

Instead, what you might want to consider is using xConnect to register the goal/interaction.

Taken from Jason Wilkerson's great blog post on the subject:

using (var client = GetClient())
{
     var contactReference = new IdentifiedContactReference("twitter", "longhorntaco");
     var contact = client.Get(contactReference, new ExpandOptions() { FacetKeys = { "Personal" } });


 if (contact != null)
 {
     // Item ID of the "Enter Store" Offline Channel at
     // /sitecore/system/Marketing Control Panel/Taxonomies/Channel/Offline/Store/Enter store
     var enterStoreChannelId = Guid.Parse("{3FC61BB8-0D9F-48C7-9BBD-D739DCBBE032}");
     var userAgent = "xConnectIntro Console App";

     var interaction = new Interaction(contact, InteractionInitiator.Contact, enterStoreChannelId, userAgent);

     var productPurchaseOutcomeId = Guid.Parse("{9016E456-95CB-42E9-AD58-997D6D77AE83}");
     var outcome = new Outcome(productPurchaseOutcomeId, DateTime.UtcNow, "USD", 42.95m);

     interaction.Events.Add(outcome);

     client.AddInteraction(interaction);
     client.Submit();
 }
}
2

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.