18

I have found that I can assign a goal or a page event to get triggered when a user hits a given page. So, we have a thank you page a user is redirected to after submitting a form and that successfully triggers our goal.

However, we now have a new requirement to do this without the page redirect... Is there a way I can trigger this goal through some standard API or through JavaScript directly?

9

There are two ways to register goals from JavaScript that I am aware of:

Federated Experience Manager

Sitecore FXM allows you to make visits and trigger events from external websites.

There's an FXM feature (that is not commonly used), allowing you to combine simultaneous site visits from the same browser into a single interaction. Visits will be combined even if one visit was on your Sitecore website, and the other on an external FXM-powered website. You should enable this feature with the setting FXM.ShareSessionsWhenPossible located in Sitecore.FXM.config.

Since your "external" site will actually be your Sitecore instance, it will be hosted on the same domain name, and hence, all FXM API calls will be treated as the same logical visit. In other words, you shouldn't be worried about this compatibility table.

Once you set up FXM, you can record goals using the following FXM code:

SCBeacon.trackGoal(“Goal name”);

You can find more API details on this official documentation page:

https://doc.sitecore.net/sitecore_experience_platform/digital_marketing/federated_experience_manager/configuring/fxm_javascript_api

Custom web endpoint

If using FXM wouldn't work for you, then you should use the server-side goal registration API. You will need to expose it as a web service endpoint (WebAPI, MVC, .ashx, whatever suits you best).

Here's the proper way to register a goal server-side:

if (Sitecore.Analytics.Tracker.IsActive)
{
    if (Sitecore.Analytics.Tracker.Current.CurrentPage != null)
    {
        var goalId = new Sitecore.Data.ID("{INSERT-GOAL-ID-HERE}");

        Sitecore.Analytics.Data.Items.PageEventItem goalToTrigger =
            Sitecore.Analytics.Tracker.DefinitionItems.PageEvents[goalId];

        if (goalToTrigger != null)
        {
            Sitecore.Analytics.Model.PageEventData eventData =
                Sitecore.Analytics.Tracker.Current.CurrentPage.Register(goalToTrigger);
        }
        else
        {
            Sitecore.Diagnostics.Log.Error("Goal with ID " + goalId + " does not exist", this);
        }
    }
    else
    {
        Sitecore.Diagnostics.Log.Error("Tracker.Current.CurrentPage is null", this);
    }
}
else
{
    Sitecore.Diagnostics.Log.Warn("The tracker is not active. Unable to register the goal.", this);
}
13

Yes you can. In this post http://ggullentops.blogspot.be/2016/02/integrating-addthis-with-sitecore-goals.html I described how you can trigger a goal from AddThis (so actually using javascript to call a controller).

A snippet from the controller code:

var goalItem = ... // get goal item from Sitecore
var visit = Tracker.Current;
var page = Tracker.Current.Session.Interaction.PreviousPage;
var registerTheGoal = new PageEventItem(goalItem);
var eventData = page.Register(registerTheGoal);
eventData.Data = goalItem["Description"];
eventData.ItemId = goalItem.ID.Guid;
eventData.DataKey = goalItem.Paths.Path;
Tracker.Current.Interaction.AcceptModifications();
Tracker.Current.CurrentPage.Cancel(); 
  • Ah, nice! Do I need to register any type of custom route to be able to call this controller method through javascript? – LonghornTaco Oct 5 '16 at 15:23
  • We registered the route, but I'm actually not sure anymore if it is really needed. – Gatogordo Oct 5 '16 at 15:50

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.