Register a page event programmatically

Current version: 8.1

In the Sitecore Experience Platform, a page event can be triggered by the system or triggered by a visitor to your website. For example, a visitor completing a campaign sign-up form and clicking submit could trigger an event, such as a goal conversion event or a campaign event.

All page event definitions are stored in the following location in the Sitecore content tree:

/sitecore/system/Settings/Analytics/Page Events

You can configure each page event definition stored here to trigger rules and other actions, and you can associate engagement value points with each page event. In the Content Editor, you can create and configure your own custom page event definitions by adding a new Page Event item to the list of existing definitions.

You can register page events on any page or object that implements the Sitecore.Analytics.Tracking.IPageContext interface. For example, this could be the current page, the previous page, or any other page or object that implements this interface.

To register a page event:

  1. Import the following namespaces into your solution:

    • Sitecore.Analytics

    • Sitecore.Analytics.Data

  2. Use the RegisterPageEvent method to register a page event:

    private void RegisterPageEvent(string name, Guid definitionId, Guid itemId, string data, string text)

    This method takes the following parameters:

    Parameter

    Description

    Examples

    name

    Name of the page event to be registered.

    Download, Search, Form Submitted.

    definitionId

    Unique identifier of the page event.

    Item ID of the page event item.

    {F358D040-256F-4FC6-B2A1-739ACA2B2983}

    itemId

    Unique identifier of the page containing the page event.

    Item ID of the content item.

    {110D559F-DEA5-42EA-9C1C-8A5DF7E70EF9}

    data

    Custom data or other information related to the page event.

    For example, in the Experience Profile the data field is used to retrieve search keywords for search page events and campaign IDs for campaign page events.

    Search, Brochure.pdf or 7 seconds.

    text

    Text that describes the page event in a readable format. This text can be an error message or an information message.

    No results found, Brochure downloaded, Request took 7.263ms to complete.

  3. Include the following code to check that all the necessary preconditions have been met before registering a page event. For example, to check for the current session, the current page, and the interaction related to the page event:

    RequestResponse
          Assert.IsNotNull(Tracker.Current, "Tracker.Current");
          Assert.IsNotNull(Tracker.Current.Session, "Tracker.Current.Session");
          var interaction = Tracker.Current.Session.Interaction;
          Assert.IsNotNull(interaction, "Tracker.Current.Session.Interaction");
          Assert.IsNotNull(interaction.CurrentPage, 
    "Tracker.Current.Session.Interaction.CurrentPage");
  4. To register the page event, include the following code:

    RequestResponse
    var pageEventData = new PageEventData(name, definitionId)
          {
            ItemId = itemId,
            Data = data,
            Text = text
          };

    The following line ensures that the page event is registered on the current page:

    RequestResponse
    interaction.CurrentPage.Register(pageEventData);

Do you have some feedback for us?

If you have suggestions for improving this article,