---
title: "インタラクションイベントを取得"
slug: "get-interaction-events"
abstract: "タイプ、定義ID、またはキャンペーンIDでインタラクションイベントを取得する方法。特定のページやイベント、キャンペーンに関連するイベントの取得方法。"
---
# インタラクションイベントを取得

イベントはInteractionオブジェクトのEventsプロパティ上で利用可能である。すべての相互作用には少なくとも一つのイベントがある。

## イベントの種類別に取得

以下の例は、タイムスタンプ順に、相互作用中に発生したGoalイベントのリストを取得する方法を示しています。

using Sitecore.XConnect.Collection.Model;
using Sitecore.XConnect;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Linq;
using System.Collections.ObjectModel;
using Sitecore.XConnect.Operations;
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 Sitecore.XConnect.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 Sitecore.XConnect.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.Collections.Generic;
using System.Threading.Tasks;
using System.Linq;
using System.Collections.ObjectModel;
using Sitecore.XConnect.Operations;
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 Sitecore.XConnect.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 Sitecore.XConnect.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
 }
 }
 }
 }
}

## ページビューイベントを見る

ウェブ上のやり取りでは、ページビューは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 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 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

あるページの文脈でイベントがトリガーされた場合、そのイベントのParentEventIdがPageViewEventのIdとなります。以下の例は、ページのリストを順に進めて、そのページでトリガーされたすべてのイベントを取得する方法を示しています。

using Sitecore.XConnect.Collection.Model;
using Sitecore.XConnect;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Linq;
using System.Collections.ObjectModel;
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 Sitecore.XConnect.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 = eventMappageView.Id.OfType<Goal>();
 var eventAssociatedWithPageView = eventMappageView.Id.OfType<Event>().Where(x => x.DefinitionId == eventGuid);
 }
 }
 catch (XdbExecutionException ex)
 {
 // Manage exceptions
 }
 }
 }

 // Sync example
 public async 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 Sitecore.XConnect.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 = eventMappageView.Id.OfType<Goal>();
 var eventAssociatedWithPageView = eventMappageView.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.Collections.Generic;
using System.Threading.Tasks;
using System.Linq;
using System.Collections.ObjectModel;
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 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 async 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 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](https://doc.sitecore.com/xp/ja/developers/93/sitecore-experience-platform/marketing-operations-api.html "Marketing Operations API")を使ってイベントやキャンペーンの定義項目を取得することができます。Sitecore以外の文脈では、Reference Data Service Client APIをご利用ください。

> !注ランダムなGUIDでイベントをトリガーすることが可能です。しかし、これは報告に悪影響を及ぼします。