インタラクションイベントを取得する

Version:
日本語翻訳に関する免責事項

このページの翻訳はAIによって自動的に行われました。可能な限り正確な翻訳を心掛けていますが、原文と異なる表現や解釈が含まれる場合があります。正確で公式な情報については、必ず英語の原文をご参照ください。

イベントは、InteractionオブジェクトのEventsプロパティで使用できます。すべてのインタラクションには、少なくとも1つのイベントがあります。

種類別にイベントを取得する

次の例は、インタラクション中に発生したGoalイベントのリストをタイムスタンプ順に取得する方法を示しています。

using Sitecore.XConnect.Collection.Model;
using Sitecore.XConnect;
using System;
using System.Linq;
using Sitecore.XConnect.Client;

namespace Documentation
{
    public class GetInteractionEvents
    {
        // Async example
        public async void ExampleAsync()
        {
            using (Sitecore.XConnect.Client.XConnectClient client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
            {
                try
                {

                    // Contact reference from ID
                    var contactRef = new Sitecore.XConnect.ContactReference(Guid.Parse("B9814105-1F45-E611-82E6-34E6D7117DCB"));
                    Sitecore.XConnect.InteractionReference interactionRef = new Sitecore.XConnect.InteractionReference(contactRef, Guid.Parse("DA2DA5F0-4348-E611-82E7-34E6D7117DCB"));

                    var interactionTask = client.GetAsync<Interaction>(interactionRef, new InteractionExecutionOptions(new InteractionExpandOptions(IpInfo.DefaultFacetKey)));

                    Interaction interaction = await interactionTask;

                    if (interaction != null)
                    {
                        var events = interaction.Events;

                        // Get all goals
                        var goals = interaction.Events.OfType<Goal>().OrderBy(ev => ev.Timestamp).ToList();
                    }
                }
                catch (Exception ex)
                {
                    // Manage exceptions
                }
            }
        }

        // Sync example
        public void ExampleSync()
        {
            using (Sitecore.XConnect.Client.XConnectClient client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
            {
                try
                {

                    // Contact reference from ID
                    var contactRef = new Sitecore.XConnect.ContactReference(Guid.Parse("B9814105-1F45-E611-82E6-34E6D7117DCB"));
                    Sitecore.XConnect.InteractionReference interactionRef = new Sitecore.XConnect.InteractionReference(contactRef, Guid.Parse("DA2DA5F0-4348-E611-82E7-34E6D7117DCB"));

                    Interaction interaction = client.Get<Interaction>(interactionRef, new InteractionExecutionOptions(new InteractionExpandOptions(IpInfo.DefaultFacetKey)));

                    if (interaction != null)
                    {
                        var events = interaction.Events;

                        // Get all goals
                        var goals = interaction.Events.OfType<Goal>().OrderBy(ev => ev.Timestamp).ToList();
                    }
                }
                catch (Exception ex)
                {
                    // Manage exceptions
                }
            }
        }
    }
}

定義IDによるイベントの取得

特定の目標、結果、ページイベントを取得するには、イベントのDefinitionIdプロパティでフィルタします。このプロパティは、イベントのトリガー時に使用されたSitecoreの定義アイテムのIDです。

DownloadEventなどの一部のイベント・モデルには、イベントがトリガーされるたびに使用される固定定義IDがあり、これは、イベント・モデルとイベント定義の間に1対1の関係があることを意味します。ただし、Goalイベント モデルは、任意の目標定義アイテムで使用できます。

次の例では、定義がID {A4364105-1F45-E611-82E6-34E6D7117DCB} のすべての目標が選択されます。

using Sitecore.XConnect.Collection.Model;
using Sitecore.XConnect;
using System;
using System.Linq;
using Sitecore.XConnect.Client;

namespace Documentation
{
    public class GetEventByDefinition
    {
        // Async example
        public async void ExampleAsync()
        {
            using (Sitecore.XConnect.Client.XConnectClient client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
            {
                try
                {

                    // Contact reference from ID
                    var contactRef = new Sitecore.XConnect.ContactReference(Guid.Parse("B9814105-1F45-E611-82E6-34E6D7117DCB"));
                    Sitecore.XConnect.InteractionReference interactionRef = new Sitecore.XConnect.InteractionReference(contactRef, Guid.Parse("DA2DA5F0-4348-E611-82E7-34E6D7117DCB"));

                    var interactionTask = client.GetAsync<Interaction>(interactionRef, new InteractionExecutionOptions(new InteractionExpandOptions(IpInfo.DefaultFacetKey)));

                    Interaction interaction = await interactionTask;

                    if (interaction != null)
                    {
                        var events = interaction.Events;

                        // Get all goals with definition ID {A4364105-1F45-E611-82E6-34E6D7117DCB}
                        var goals = interaction.Events.OfType<Goal>().Where(x => x.DefinitionId == Guid.Parse("A4364105-1F45-E611-82E6-34E6D7117DCB"));
                    }
                }
                catch (XdbExecutionException ex)
                {
                    // Manage exceptions
                }
            }
        }

        // Sync example
        public void ExampleSync()
        {
            using (Sitecore.XConnect.Client.XConnectClient client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
            {
                try
                {

                    // Contact reference from ID
                    var contactRef = new Sitecore.XConnect.ContactReference(Guid.Parse("B9814105-1F45-E611-82E6-34E6D7117DCB"));
                    Sitecore.XConnect.InteractionReference interactionRef = new Sitecore.XConnect.InteractionReference(contactRef, Guid.Parse("DA2DA5F0-4348-E611-82E7-34E6D7117DCB"));

                    Interaction interaction = client.Get<Interaction>(interactionRef, new InteractionExecutionOptions(InteractionExpandOptions(IpInfo.DefaultFacetKey)));

                    if (interaction != null)
                    {
                        var events = interaction.Events;

                        // Get all goals with definition ID {A4364105-1F45-E611-82E6-34E6D7117DCB}
                        var goals = interaction.Events.OfType<Goal>().Where(x => x.DefinitionId == Guid.Parse("A4364105-1F45-E611-82E6-34E6D7117DCB"));
                    }
                }
                catch (XdbExecutionException ex)
                {
                    // Manage exceptions
                }
            }
        }
    }
}

ページビューイベントを取得する

Webインタラクションの場合、ページビューはPageViewEventモデルで表されます。次の例は、PageViewEventイベントの一覧を取得する方法を示しています。

using Sitecore.XConnect.Collection.Model;
using Sitecore.XConnect;
using System;
using System.Linq;
using Sitecore.XConnect.Client;

namespace Documentation
{
    public class PageView
    {
        public async void Example()
        {
            using (Sitecore.XConnect.Client.XConnectClient client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
            {
                try
                {

                    // Contact reference from ID
                    var contactRef = new Sitecore.XConnect.ContactReference(Guid.Parse("B9814105-1F45-E611-82E6-34E6D7117DCB"));
                    Sitecore.XConnect.InteractionReference interactionRef = new Sitecore.XConnect.InteractionReference(contactRef, Guid.Parse("DA2DA5F0-4348-E611-82E7-34E6D7117DCB"));

                    var interactionTask = client.GetAsync<Interaction>(interactionRef, new InteractionExecutionOptions(new InteractionExpandOptions(IpInfo.DefaultFacetKey)));

                    var interaction = await interactionTask;

                    var events = interaction.Events;

                    var pageViews = interaction.Events.OfType<PageViewEvent>().OrderBy(ev => ev.Timestamp).ToList();
                }
                catch (XdbExecutionException ex)
                {
                    // Handle exception
                }
            }
        }

        // Sync example
        public void ExampleSync()
        {
            using (Sitecore.XConnect.Client.XConnectClient client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
            {
                try
                {

                    // Contact reference from ID
                    var contactRef = new Sitecore.XConnect.ContactReference(Guid.Parse("B9814105-1F45-E611-82E6-34E6D7117DCB"));
                    Sitecore.XConnect.InteractionReference interactionRef = new Sitecore.XConnect.InteractionReference(contactRef, Guid.Parse("DA2DA5F0-4348-E611-82E7-34E6D7117DCB"));

                    var interaction = client.Get<Interaction>(interactionRef, new InteractionExecutionOptions(new InteractionExpandOptions(IpInfo.DefaultFacetKey)));

                    var events = interaction.Events;

                    var pageViews = interaction.Events.OfType<PageViewEvent>().OrderBy(ev => ev.Timestamp).ToList();
                }
                catch (XdbExecutionException ex)
                {
                    // Handle exception
                }
            }
        }
    }
}

シーケンス番号によるページビューの取得

VisitPageIndexの直接の代替品はありません。ページビューをTimestamp順に並べ替えるには、ElementAt() 拡張機能を使用します。例えば:

var pageViews = interaction.Events.OfType<PageViewEvent>().OrderBy(ev => ev.Timestamp).ToList();
var event = pageViewEvents.ElementAt(3); // Gets fourth page view

特定のページで発生したイベントを取得する

連絡先がページにアクセスすると、PageViewEventがトリガーされます。そのページのコンテキストでは、次のような追加のイベントがトリガーされることもあります。

  • Goal

  • Outcome

  • DownloadEvent

  • SearchEvent

  • CampaignEvent

  • 汎用 Event

イベントのコンテキストでイベントがトリガーされた場合、そのイベントのParentEventIdPageViewEventIdになります。次の例は、ページのリストを反復処理し、そのページでトリガーされたすべてのイベントを取得する方法を示しています。

using Sitecore.XConnect.Collection.Model;
using Sitecore.XConnect;
using System;
using System.Linq;
using Sitecore.XConnect.Client;

namespace Documentation
{
    public class EventsOnPage
    {
        // Async example
        public async void ExampleAsync()
        {
            using (Sitecore.XConnect.Client.XConnectClient client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
            {
                try
                {

                    // Contact reference from ID
                    var contactRef = new Sitecore.XConnect.ContactReference(Guid.Parse("B9814105-1F45-E611-82E6-34E6D7117DCB"));
                    Sitecore.XConnect.InteractionReference interactionRef = new Sitecore.XConnect.InteractionReference(contactRef, Guid.Parse("DA2DA5F0-4348-E611-82E7-34E6D7117DCB"));


                    var interactionTask = client.GetAsync<Sitecore.XConnect.Interaction>(interactionRef, new InteractionExecutionOptions(new InteractionExpandOptions(IpInfo.DefaultFacetKey)));

                    var interaction = await interactionTask;

                    // This particular example is web-focused and is getting all events with a parent event of type PageViewEvent
                    var pageViews = interaction.Events.OfType<PageViewEvent>().OrderBy(ev => ev.Timestamp).ToList();

                    // Create a map of events - this is the most efficient way to work with an interaction that
                    // has many events. It is a general recommendation and not specific to Sitecore.
                    var eventMap = interaction.Events.Where(ev => ev.ParentEventId.HasValue).ToLookup(ev => ev.ParentEventId.Value);
                    var eventGuid = Guid.Parse("c91ce6eb-c346-447e-b65d-1f0f90b520e5"); // Replace with the ID of a real event definition item

                    // For each page view, get any events where ParentEventId equals ID of the page view event
                    // and the event has a specific definition ID
                    foreach (var pageView in pageViews)
                    {
                        var goalAssociatedWithPageView = eventMap[pageView.Id].OfType<Goal>();
                        var eventAssociatedWithPageView = eventMap[pageView.Id].OfType<Event>().Where(x => x.DefinitionId == eventGuid);
                    }
                }
                catch (XdbExecutionException ex)
                {
                    // Manage exceptions
                }
            }
        }

        // Sync example
        public void ExampleSync()
        {
            using (Sitecore.XConnect.Client.XConnectClient client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
            {
                try
                {

                    // Contact reference from ID
                    var contactRef = new Sitecore.XConnect.ContactReference(Guid.Parse("B9814105-1F45-E611-82E6-34E6D7117DCB"));
                    Sitecore.XConnect.InteractionReference interactionRef = new Sitecore.XConnect.InteractionReference(contactRef, Guid.Parse("DA2DA5F0-4348-E611-82E7-34E6D7117DCB"));

                    var interaction = client.Get<Sitecore.XConnect.Interaction>(interactionRef, new InteractionExecutionOptions(new InteractionExpandOptions(IpInfo.DefaultFacetKey)));

                    // This particular example is web-focused and is getting all events with a parent event of type PageViewEvent
                    var pageViews = interaction.Events.OfType<PageViewEvent>().OrderBy(ev => ev.Timestamp).ToList();

                    // Create a map of events - this is the most efficient way to work with an interaction that
                    // has many events. It is a general recommendation and not specific to Sitecore.
                    var eventMap = interaction.Events.Where(ev => ev.ParentEventId.HasValue).ToLookup(ev => ev.ParentEventId.Value);
                    var eventGuid = Guid.Parse("c91ce6eb-c346-447e-b65d-1f0f90b520e5"); // Replace with the ID of a real event definition item

                    // For each page view, get any events where ParentEventId equals ID of the page view event
                    // and the event has a specific definition ID
                    foreach (var pageView in pageViews)
                    {
                        var goalAssociatedWithPageView = eventMap[pageView.Id].OfType<Goal>();
                        var eventAssociatedWithPageView = eventMap[pageView.Id].OfType<Event>().Where(x => x.DefinitionId == eventGuid);
                    }
                }
                catch (XdbExecutionException ex)
                {
                    // Manage exceptions
                }
            }
        }
    }
}

カスタムタイプで表されていないページイベントを取得する

特定のページイベントは、DownloadEventなどのカスタムタイプで表されます。イベントがカスタムタイプで表 されていない場合は 、基本Eventタイプを使用し、イベントの定義IDでフィルタリングします。

var eventGuid = Guid.Parse("c91ce6eb-c346-447e-b65d-1f0f90b520e5"); // Replace with the ID of a real event definition item

var event = interaction.Events.OfType<Event>().Where(x => x.DefinitionId == eventGuid);

キャンペーンIDでキャンペーンイベントを取得する

CampaignEventには、次の2つの定義IDがあります。

  • DefinitionIdはイベント定義アイテム自体のIDで、このIDは常に同じです

  • CampaignDefinitionIdはキャンペーン定義アイテムのIDです - このIDは、トリガーされたキャンペーンによって異なります

次の例では、ID {29814105-2345-E611-AAE6-34E6D7117DCB} を持つキャンペーンを参照するイベントが選択されています。

using Sitecore.XConnect.Collection.Model;
using Sitecore.XConnect;
using System;
using System.Linq;
using Sitecore.XConnect.Client;

namespace Documentation
{
    public class GetCampaignEvent
    {
        // Async example
        public async void ExampleAsync()
        {
            using (Sitecore.XConnect.Client.XConnectClient client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
            {
                try
                {

                    // Contact reference from ID
                    var contactRef = new Sitecore.XConnect.ContactReference(Guid.Parse("B9814105-1F45-E611-82E6-34E6D7117DCB"));
                    Sitecore.XConnect.InteractionReference interactionRef = new Sitecore.XConnect.InteractionReference(contactRef, Guid.Parse("DA2DA5F0-4348-E611-82E7-34E6D7117DCB"));

                    var interactionTask = client.GetAsync<Sitecore.XConnect.Interaction>(interactionRef, new InteractionExecutionOptions(new Sitecore.XConnect.InteractionExpandOptions(IpInfo.DefaultFacetKey)));

                    var interaction = await interactionTask;

                    var campaigns = interaction.Events.OfType<CampaignEvent>().Where(x => x.CampaignDefinitionId == Guid.Parse("29814105-2345-E611-AAE6-34E6D7117DCB")).ToList();
                }
                catch (XdbExecutionException ex)
                {
                    // Manage exceptions
                }
            }
        }

        // Sync example
        public void ExampleSync()
        {
            using (Sitecore.XConnect.Client.XConnectClient client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
            {
                try
                {

                    // Contact reference from ID
                    var contactRef = new Sitecore.XConnect.ContactReference(Guid.Parse("B9814105-1F45-E611-82E6-34E6D7117DCB"));
                    Sitecore.XConnect.InteractionReference interactionRef = new Sitecore.XConnect.InteractionReference(contactRef, Guid.Parse("DA2DA5F0-4348-E611-82E7-34E6D7117DCB"));

                    var interaction = client.Get<Sitecore.XConnect.Interaction>(interactionRef, new InteractionExecution Options(new Sitecore.XConnect.InteractionExpandOptions(IpInfo.DefaultFacetKey)));

                    var campaigns = interaction.Events.OfType<CampaignEvent>().Where(x => x.CampaignDefinitionId == Guid.Parse("29814105-2345-E611-AAE6-34E6D7117DCB")).ToList();
                }
                catch (XdbExecutionException ex)
                {
                    // Manage exceptions
                }
            }
        }
    }
}

イベントまたはキャンペーンに関連付けられた定義項目を取得する

Sitecoreのコンテキストでは、Marketing Operations APIを使用してイベントまたはキャンペーン定義アイテムを取得できます。Sitecore以外のコンテキストでは、Reference Data Service Client APIを使用します。

メモ

ランダムなGUIDでイベントをトリガーできます。ただし、これはレポートに悪影響を及ぼします。

この記事を改善するための提案がある場合は、 お知らせください!