---
title: "インタラクションイベントの追加"
slug: "add-interaction-events"
abstract: ""
---
# インタラクションイベントの追加

このトピックでは、[さまざまな](https://doc.sitecore.com/xp/ja/developers/105/sitecore-experience-platform/add-interactions.html "Add interactions")種類のイベントを相互作用に追加する方法を示します。イベントを相互作用に追加する際には以下の点を念頭に置いてください:

-   イベントを誤った定義(例えば、アウトカム定義と Goalを関連付けるなど)を関連付けることは可能です。プラットフォームはイベントタイプのコレクションモデルと定義項目の間に特定の関係を強制しません。データの忠実さを確保するのはあなたの責任です。
-   EngagementValueプロパティは任意で、手動設定する必要があります。トラッキングコンテキストでは、トラッカーは保存前にイベントの定義アイテムのデフォルトエンゲージメント値にエンゲージメント値を設定します。トラッキング以外のコンテキストでは、デフォルトのエンゲージメント値を使いたい場合は、[マーケティングオペレーションAPI](https://doc.sitecore.com/xp/ja/developers/105/sitecore-experience-platform/marketing-operations-api.html "Marketing Operations API")を使ってイベント定義アイテムを取得しなければなりません。

## インタラクションに目標を追加する

以下の例は、相互作用に目標を追加する方法を示しています。

using Sitecore.XConnect;
using Sitecore.XConnect.Client;
using System;

namespace Documentation
{
 public class AddGoal
 {
 public async void ExampleAsync()
 {
 using (Sitecore.XConnect.Client.XConnectClient client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
 {
 try
 {
 var contact = new Sitecore.XConnect.Contact();

 Guid channelId = Guid.NewGuid();
 string userAgent = "Sample User Agent";
 var interaction = new Sitecore.XConnect.Interaction(contact, InteractionInitiator.Brand, channelId, userAgent);

 Guid goalID = Guid.Parse("21EC2020-3AEA-4069-A2DD-08002B30309D"); // ID of goal item

 var goal = new Goal(goalID, DateTime.UtcNow);

 goal.EngagementValue = 20; // Manually setting engagement value rather than going to definition item

 interaction.Events.Add(goal);

 client.AddContact(contact);
 client.AddInteraction(interaction);

 await client.SubmitAsync();
 }
 catch (XdbExecutionException ex)
 {
 // Handle exception
 }
 }
 }

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

 Guid channelId = Guid.NewGuid();
 string userAgent = "Sample User Agent";
 var interaction = new Sitecore.XConnect.Interaction(contact, InteractionInitiator.Brand, channelId, userAgent);

 Guid goalID = Guid.Parse("21EC2020-3AEA-4069-A2DD-08002B30309D"); // ID of goal item

 var goal = new Goal(goalID, DateTime.UtcNow);

 goal.EngagementValue = 20; // Manually setting engagement value rather than going to definition item

 interaction.Events.Add(goal);

 client.AddContact(contact);
 client.AddInteraction(interaction);

 client.Submit();
 }
 catch (XdbExecutionException ex)
 {
 // Handle exception
 }
 }
 }
 }
}

## 相互作用に結果を追加する

以下の例は、相互作用に結果を加える方法を示しています。

> !注CurrencyとMonetaryValueは必須の性質ですが、結果が金銭的価値と結びついていなければString.Emptyや0.00mにパスすることができます。

using System;
using Sitecore.XConnect;
using Sitecore.XConnect.Client;

namespace Documentation
{
 public class AddOutcome
 {
 public async void Example()
 {
 using (Sitecore.XConnect.Client.XConnectClient client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
 {
 try
 {
 var contact = new Sitecore.XConnect.Contact();

 var channel = Guid.NewGuid(); // Use real channel ID from Sitecore

 var interaction = new Interaction(contact, InteractionInitiator.Brand, channel, "iTunes/9.0.3 (Macintosh; U; Intel Mac OS X 10\_6\_2; en-ca)"); // Agent is iTunes

 Guid outcomeId = Guid.Parse("21EC2020-3AEA-4069-A2DD-08002B30309D"); // ID of outcome item

 var outcome = new Sitecore.XConnect.Outcome(outcomeId, DateTime.UtcNow, "USD", 100.00m);

 interaction.Events.Add(outcome);

 client.AddContact(contact);
 client.AddInteraction(interaction);

 await client.SubmitAsync();
 }
 catch (XdbExecutionException ex)
 {
 // Handle exception
 }
 }
 }

 public void ExampleSync()
 {
 using (Sitecore.XConnect.Client.XConnectClient client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
 {
 try
 {
 var contact = new Sitecore.XConnect.Contact();

 var channel = Guid.NewGuid(); // Use real channel ID from Sitecore

 var interaction = new Interaction(contact, InteractionInitiator.Brand, channel, "iTunes/9.0.3 (Macintosh; U; Intel Mac OS X 10\_6\_2; en-ca)"); // Agent is iTunes

 Guid outcomeId = Guid.Parse("21EC2020-3AEA-4069-A2DD-08002B30309D"); // ID of outcome item

 var outcome = new Sitecore.XConnect.Outcome(outcomeId, DateTime.UtcNow, "USD", 100.00m);

 interaction.Events.Add(outcome);

 client.AddContact(contact);
 client.AddInteraction(interaction);

 client.Submit();
 }
 catch (XdbExecutionException ex)
 {
 // Handle exception
 }
 }
 }
 }
}

## PageViewEventをインタラクションに追加する

ウェブのやり取りは複数のネストされたイベントを持つことが多いです(目標とページイベントはページビューイベントの親を持ちます)。以下の例は、PageViewEventイベントを追加するウェブ訪問のシミュレーション方法を示しています。

PageViewEventではページのURLと関連するアイテムIDの両方を保存できるため、UrlプロパティにエイリアスURLやクエリ文字列を保存できます。

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

namespace Documentation
{
 public class SampleWebVisit
 {
 // Async example
 public async void Example()
 {
 using (Sitecore.XConnect.Client.XConnectClient client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
 {
 // Create a new contact and set some facets
 var contact = new Contact();

 var personalInfoFacet = new PersonalInformation()
 {
 FirstName = "Myrtle",
 LastName = "McSitecore"
 };

 client.SetPersonal(contact, personalInfoFacet);

 client.AddContact(contact);

 // Create a new interaction for the contact
 Guid channelId = Guid.NewGuid(); // Use real channel ID from Sitecore
 string userAgent = "Sample User Agent";
 Interaction webInteraction = new Interaction(contact, InteractionInitiator.Brand, channelId, userAgent);

 // Create a new web visit facet model

 var webVisitFacet = new WebVisit()
 {
 // Populate data about the web visit
 Browser = new BrowserData() { BrowserMajorName = "Chrome", BrowserMinorName = "Desktop", BrowserVersion = "22.0" },
 Language = "en",
 OperatingSystem = new OperatingSystemData() { Name = "Windows", MajorVersion = "10", MinorVersion = "4" },
 Referrer = "www.google.com",
 Screen = new ScreenData() { ScreenHeight = 1080, ScreenWidth = 685 },
 SearchKeywords = "sitecore",
 SiteName = "website"
 };

 var itemId = Guid.NewGuid();
 var itemVersion = 5;

 // First page view
 PageViewEvent
pageView = new PageViewEvent(new DateTime(2016, 10, 10, 13, 20,
22).ToUniversalTime(), itemId, itemVersion, "en")

                {

                    ItemLanguage = "en",

                    Duration = new
TimeSpan(3000)

                };
 webInteraction.Events.Add(pageView);

 var secondItemId = Guid.NewGuid();
 var secondItemVersion = 2;

 // Second page view
 PageViewEvent pageView2 = new
PageViewEvent(new DateTime(2016, 10, 10, 13, 21, 22).ToUniversalTime(),
secondItemId, secondItemVersion, "en")

                {

                    ItemLanguage = "en",

             Duration = new
TimeSpan(3200)

                };
 webInteraction.Events.Add(pageView2);

 // First goal, associated with second page view
 Goal goal1 = new Goal(Guid.NewGuid(), new DateTime(2016, 10, 10, 13, 22, 22).ToUniversalTime())
 {
 ParentEventId = pageView2.Id
 };
 webInteraction.Events.Add(goal1);

 var thirdItemId = Guid.NewGuid();
 var thirdItemVersion = 2;

 // Third page view
 PageViewEvent pageView3 = new
PageViewEvent(new DateTime(2016, 10, 10, 13, 22, 22).ToUniversalTime(),
thirdItemId, thirdItemVersion, "en")

                {

                    ItemLanguage = "en",

             Duration = new
TimeSpan(1200)

                };

 webInteraction.Events.Add(pageView3);

 // Set web visit facet on interaction
 client.SetWebVisit(webInteraction, webVisitFacet);

 // Add interaction
 client.AddInteraction(webInteraction);

 // Submit contact and interaction
 await client.SubmitAsync();
 }
 }

 // Sync example
 public void ExampleSync()
 {
 using (Sitecore.XConnect.Client.XConnectClient client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
 {
 // Create a new contact and set some facets
 var contact = new Contact();

 var personalInfoFacet = new PersonalInformation()
 {
 FirstName = "Myrtle",
 LastName = "McSitecore"
 };

 client.SetPersonal(contact, personalInfoFacet);

 client.AddContact(contact);

 // Create a new interaction for the contact
 Guid channelId = Guid.NewGuid(); // Use real channel ID from Sitecore
 string userAgent = "Sample User Agent";
 Interaction webInteraction = new Interaction(contact, InteractionInitiator.Brand, channelId, userAgent);

 // Create a new web visit facet model

var webVisitFacet = new WebVisit()

                {

                    // Populate data about the
web visit

                  Browser = new
BrowserData() { BrowserMajorName = "Chrome", BrowserMinorName = "Desktop",
BrowserVersion = "22.0" },

                    Language = "en",

                    OperatingSystem = new
OperatingSystemData() { Name = "Windows", MajorVersion = "10",
MinorVersion = "4" },

                    Referrer = "www.google.com",

                    Screen = new ScreenData() {
ScreenHeight = 1080, ScreenWidth = 685 },

                    SearchKeywords = "sitecore",

                    SiteName = "website"

                };

 var itemId = Guid.NewGuid();
 var itemVersion = 5;

 // First page view
 PageViewEvent pageView = new PageViewEvent(new DateTime(2016, 10, 10, 13, 20, 22).ToUniversalTime(), itemId, itemVersion, "en");

 pageView.ItemLanguage = "en";
 pageView.Duration = new TimeSpan(3000);

 webInteraction.Events.Add(pageView);

 var secondItemId = Guid.NewGuid();
 var secondItemVersion = 2;

 // Second page view
 PageViewEvent pageView2 = new
PageViewEvent(new DateTime(2016, 10, 10, 13, 21, 22).ToUniversalTime(),
secondItemId, secondItemVersion, "en")

                {
                    ItemLanguage = "en",

                    Duration = new
TimeSpan(3200)
                };

 webInteraction.Events.Add(pageView2);

 // First goal, associated with second page view
 Goal goal1 = new Goal(Guid.NewGuid(), new DateTime(2016, 10, 10, 13, 22, 22).ToUniversalTime());

 goal1.ParentEventId = pageView2.Id;

 webInteraction.Events.Add(goal1);

 var thirdItemId = Guid.NewGuid();
 var thirdItemVersion = 2;

 // Third page view
 PageViewEvent pageView3 = new PageViewEvent(new DateTime(2016, 10, 10, 13, 22, 22).ToUniversalTime(), thirdItemId, thirdItemVersion, "en");

 pageView3.ItemLanguage = "en";
 pageView3.Duration = new TimeSpan(1200);

 webInteraction.Events.Add(pageView3);

 // Set web visit facet on interaction
 client.SetWebVisit(webInteraction, webVisitFacet);

 // Add interaction
 client.AddInteraction(webInteraction);

 // Submit contact and interaction
 client.Submit();
 }
 }
 }
}

### ページビューイベントの子としてイベントを追加する

目標をページビューに関連付けるには、前の例で示したように、目標のParentEventIdをページビューのIdに設定します。

// Second page view
PageViewEvent pageView2 = new PageViewEvent(new DateTime(2016, 10, 10, 13, 21, 22).ToUniversalTime(), secondItemId, secondItemVersion, "en")
 {
 ItemLanguage = "en",
 Duration = new TimeSpan(3200)
 };

webInteraction.Events.Add(pageView2);

// First goal, associated with second page view
Goal goal1 = new Goal(Guid.NewGuid(), new DateTime(2016, 10, 10, 13, 22, 22).ToUniversalTime())
 {
 ParentEventId = pageView2.Id
 };

webInteraction.Events.Add(goal1);

var thirdItemId = Guid.NewGuid();
var thirdItemVersion = 2;

### ページイベントの追加

ページイベントは */sitecore/system/Settings/Analytics/Page Events*下に保存され、「ユーザーが検索を行った」や「ページが遅い」など、一般的なウェブ固有のイベントを表します。ページイベントをトリガーする際は、ParentEventIdプロパティを設定してPageViewEventと関連付けてください。

すべてのページイベントが専門クラスで表現されているわけではありません。専門クラスを持たないイベントの場合は、基本Eventクラスを使い、イベント定義項目のIDを渡してください。

#### インタラクションにDownloadEventを追加する

以下の例はダウンロードイベントをトリガーする方法を示しています。コンストラクタに渡されるItemIdは、ダウンロードされたメディアアイテムのIDです。

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

namespace Documentation
{
 public class AddDownloadEvent
 {
 public async void ExampleAsync()
 {
 using (Sitecore.XConnect.Client.XConnectClient client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
 {
 try
 {
 var newContact = new Sitecore.XConnect.Contact();
 client.AddContact(newContact);

 Guid channelId = Guid.NewGuid();
 string userAgent = "Sample User Agent";

 var interaction = new Sitecore.XConnect.Interaction(newContact, InteractionInitiator.Brand, channelId, userAgent);

 var fakeItemID = Guid.NewGuid();
 var fakeItemVersion = 3;

 Sitecore.XConnect.Collection.Model.PageViewEvent pageView = new PageViewEvent(DateTime.UtcNow, fakeItemID, fakeItemVersion, "en")
 {
 Duration = new TimeSpan(0, 0, 30),
 Url = "/my/test/page?with=querystring"
 };

 interaction.Events.Add(pageView);

 var guidOfDownload = Guid.NewGuid(); // Replace with GUID of media item that was downloaded

 DownloadEvent download = new DownloadEvent(DateTime.UtcNow, guidOfDownload)
 {
 ParentEventId = pageView.Id
 };

 interaction.Events.Add(download);

 client.AddInteraction(interaction);

 await client.SubmitAsync();
 }
 catch (XdbExecutionException ex)
 {
 // Handle exception
 }
 }
 }

 // Sync example
 public void ExampleSync()
 {
 using (Sitecore.XConnect.Client.XConnectClient client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
 {
 try
 {
 var newContact = new Sitecore.XConnect.Contact();
 client.AddContact(newContact);

 Guid channelId = Guid.NewGuid();
 string userAgent = "Sample User Agent";

 var interaction = new Sitecore.XConnect.Interaction(newContact, InteractionInitiator.Brand, channelId, userAgent);

 var fakeItemID = Guid.NewGuid();
 var fakeItemVersion = 3;

 Sitecore.XConnect.Collection.Model.PageViewEvent pageView = new PageViewEvent(DateTime.UtcNow, fakeItemID, fakeItemVersion, "en")
 {
 Duration = new TimeSpan(0, 0, 30),
 Url = "/my/test/page?with=querystring"
 };

 interaction.Events.Add(pageView);

 var guidOfDownload = Guid.NewGuid(); // Replace with GUID of media item that was downloaded

 DownloadEvent download = new DownloadEvent(DateTime.UtcNow, guidOfDownload)
 {
 ParentEventId = pageView.Id
 };

 interaction.Events.Add(download);

 client.AddInteraction(interaction);

 client.Submit();
 }
 catch (XdbExecutionException ex)
 {
 // Handle exception
 }
 }
 }
 }
}

#### インタラクションにSearchEventを追加する

以下の例は検索イベントの登録方法を示しています。 Keywordプロパティは必須ではありません。

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

namespace Documentation
{
 public class AddSearchEvent
 {
 public async void ExampleAsync()
 {
 using (Sitecore.XConnect.Client.XConnectClient client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
 {
 try
 {
 var newContact = new Sitecore.XConnect.Contact();
 client.AddContact(newContact);

 Guid channelId = Guid.NewGuid();
 string userAgent = "Sample User Agent";

 var interaction = new Sitecore.XConnect.Interaction(newContact, InteractionInitiator.Brand, channelId, userAgent);

 var fakeItemID = Guid.NewGuid();
 var fakeItemVersion = 3;

 Sitecore.XConnect.Collection.Model.PageViewEvent pageView = new PageViewEvent(DateTime.UtcNow, fakeItemID, fakeItemVersion, "en")
 {
 Duration = new TimeSpan(0, 0, 30),
 Url = "/my/test/page?with=querystring"
 };

 interaction.Events.Add(pageView);

 var search = new SearchEvent(DateTime.UtcNow)
 {
 Keywords = "xConnect"
 };

 interaction.Events.Add(search);

 client.AddInteraction(interaction);

 await client.SubmitAsync();
 }
 catch (XdbExecutionException ex)
 {
 // Handle exception
 }
 }
 }

 // Sync example
 public void ExampleSync()
 {
 using (Sitecore.XConnect.Client.XConnectClient client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
 {
 try
 {
 var newContact = new Sitecore.XConnect.Contact();
 client.AddContact(newContact);

 Guid channelId = Guid.NewGuid();
 string userAgent = "Sample User Agent";

 var interaction = new Sitecore.XConnect.Interaction(newContact, InteractionInitiator.Brand, channelId, userAgent);

 var fakeItemID = Guid.NewGuid();
 var fakeItemVersion = 3;

 Sitecore.XConnect.Collection.Model.PageViewEvent pageView = new PageViewEvent(DateTime.UtcNow, fakeItemID, fakeItemVersion, "en")
 {
 Duration = new TimeSpan(0, 0, 30),
 Url = "/my/test/page?with=querystring"
 };

 interaction.Events.Add(pageView);

 var search = new SearchEvent(DateTime.UtcNow)
 {
 Keywords = "xConnect"
 };

 interaction.Events.Add(search);

 client.AddInteraction(interaction);

 client.Submit();
 }
 catch (XdbExecutionException ex)
 {
 // Handle exception
 }
 }
 }
 }
}

#### CampaignEventをインタラクションに追加する

以下の例はキャンペーンの発動方法を示しています。

イベントは同じイベントが**CampaignEventID**行われており、キャンペーンが発動されています。ただし、イベントに関連するキャンペーンは異なり、CampaignDefinitionIdプロパティで表現されます。

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

namespace Documentation
{
 public class AddCampaignEvent
 {
 public async void ExampleAsync()
 {
 using (Sitecore.XConnect.Client.XConnectClient client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
 {
 try
 {
 var newContact = new Sitecore.XConnect.Contact();
 client.AddContact(newContact);

 Guid channelId = Guid.NewGuid();
 string userAgent = "Sample User Agent";

 var interaction = new Sitecore.XConnect.Interaction(newContact, InteractionInitiator.Brand, channelId, userAgent);

 var fakeItemID = Guid.NewGuid();
 var fakeItemVersion = 3;

 Sitecore.XConnect.Collection.Model.PageViewEvent pageView = new PageViewEvent(DateTime.UtcNow, fakeItemID, fakeItemVersion, "en")
 {
 Duration = new TimeSpan(0, 0, 30),
 Url = "/my/test/page?with=querystring"
 };

 interaction.Events.Add(pageView);

 var campaignDefinition = Guid.NewGuid(); // Replace with real campaign ID

 var campaign = new CampaignEvent(campaignDefinition, DateTime.UtcNow);

 interaction.Events.Add(campaign);

 client.AddInteraction(interaction);

 await client.SubmitAsync();
 }
 catch (XdbExecutionException ex)
 {
 // Handle exception
 }
 }
 }

 // Sync example
 public void ExampleSync()
 {
 using (Sitecore.XConnect.Client.XConnectClient client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
 {
 try
 {
 var newContact = new Sitecore.XConnect.Contact();
 client.AddContact(newContact);

 Guid channelId = Guid.NewGuid();
 string userAgent = "Sample User Agent";

 var interaction = new Sitecore.XConnect.Interaction(newContact, InteractionInitiator.Brand, channelId, userAgent);

 var fakeItemID = Guid.NewGuid();
 var fakeItemVersion = 3;

 Sitecore.XConnect.Collection.Model.PageViewEvent pageView = new PageViewEvent(DateTime.UtcNow, fakeItemID, fakeItemVersion, "en")
 {
 Duration = new TimeSpan(0, 0, 30),
 Url = "/my/test/page?with=querystring"
 };

 interaction.Events.Add(pageView);

 var campaignDefinition = Guid.NewGuid(); // Replace with real campaign ID

 var campaign = new CampaignEvent(campaignDefinition, DateTime.UtcNow);

 interaction.Events.Add(campaign);

 client.AddInteraction(interaction);

 client.Submit();
 }
 catch (XdbExecutionException ex)
 {
 // Handle exception
 }
 }
 }
 }
}