インタラクションイベントを追加する
このページの翻訳はAIによって自動的に行われました。可能な限り正確な翻訳を心掛けていますが、原文と異なる表現や解釈が含まれる場合があります。正確で公式な情報については、必ず英語の原文をご参照ください。
このトピックでは、インタラクションにさまざまなタイプのイベントを追加する方法を示します。インタラクションにイベントを追加する際には、以下の点に注意してください。
-
イベントを誤った定義に関連付ける可能性があります (たとえば、結果定義をGoalに関連付けるなど)。プラットフォームでは、イベントの種類のコレクション モデルと定義アイテムの間に特定の関係は強制されません。データの忠実性を確保するのはお客様の責任です。
-
EngagementValueプロパティはオプションであり、手動で設定する必要があります。トラッキングコンテキストでは、トラッカーは保存する前に、エンゲージメント値をイベントの定義アイテムのデフォルトのエンゲージメント値に設定します。トラッキング以外のコンテキストでは、デフォルトのエンゲージメント値を使用する場合は、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イベントの追加を含む、Web訪問をシミュレートする方法を示しています。
この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の下に格納され、「ユーザーが検索を実行した」や「ページが遅い」など、一般的なWeb固有のイベントを表します。ページ イベントをトリガーするときは、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の追加
次の例は、キャンペーンをトリガーする方法を示しています。
同じイベントが発生している (キャンペーンがトリガーされている) CampaignEventのイベント ID。ただし、イベントに関連付けられたキャンペーンは異なり、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 } } } } }