1. インタラクションの集約

チュートリアル: インタラクション パイプラインを使用してレポート データを集計および保存する

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

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

このトピックでは、インタラクションのユーザーエージェントに対するすべての訪問の合計数と合計値をグループ化する従来のインタラクション集約プロセッサを作成する方法を示します。

メモ

パフォーマンスを向上させるために、クラシックインタラクションプロセッサを バッチインタラクションプロセッサ にアップグレードすることをお勧めします。

次のものを作成します。

  • ディメンションテーブルは、IDを持つ一意のユーザーエージェントのリストを格納する参照テーブルです。

  • ファクトテーブルは、そのユーザーエージェントによって生成された合計訪問数と値に対するユーザーエージェントIDを保存します。

  • ファクト クラスとディメンション クラス: ファクト テーブルとディメンション テーブル内のデータをモデル化します。

  • インタラクション集計プロセッサは、ファクト・クラスとディメンション・クラスを使用して、レポート・データベース内のテーブルを設定します。

ディメンション クラスとテーブルの作成

ディメンション テーブルには、ユーザー エージェント文字列の一覧が格納されます。各ユーザーエージェントには一意のIDがあります。ディメンション テーブルは、ユーザー エージェントIDに対する訪問の合計数とすべての訪問の合計値を格納したファクト テーブルによって参照されます。

ディメンション・クラスの作成

すべてのディメンションには、そのディメンションに固有のキーと値クラスがあります (この場合、それらはUserAgentNameKey (キーの例: -1743424285) とUserAgentNameValue (値の例: Chrome) です。

  1. ディメンション・キー・クラスを作成します。

    namespace Documentation.Examples
    {
        using Sitecore;
        using Sitecore.Analytics.Aggregation.Data.Model;
        using Sitecore.Analytics.Core;
    
        public class UserAgentNameKey : DictionaryKey
        {
            private readonly Hash32 userAgentNameId;
    
            public Hash32 UserAgentNameId
            {
                get
                {
                    return this.userAgentNameId;
                }
            }
            public UserAgentNameKey(Hash32 userAgentNameId)
            {
                this.userAgentNameId = userAgentNameId;
            }
        }
    }
  2. ディメンション値クラスを作成します。

    namespace Documentation.Examples
    {
        using Sitecore;
        using Sitecore.Analytics.Aggregation.Data.Model;
        using Sitecore.Analytics.Core;
    
        public class UserAgentNameValue : DictionaryValue
        {
            private readonly string userAgentName;
    
            [CanBeNull]
            public string UserAgentName
            {
                get
                {
                    return this.userAgentName;
                }
            }
    
            public UserAgentNameValue([CanBeNull] string userAgentName)
            {
                this.userAgentName = userAgentName;
            }
        }
    }
  3. UserAgentNameKeyクラスとUserAgentNameValueクラスを使用するディメンション クラスを作成します。

    namespace Documentation.Examples
    {
        using Sitecore;
        using Sitecore.Analytics.Aggregation.Data.Model;
        using Sitecore.Analytics.Core;
    
        public class UserAgentName : Dimension<UserAgentNameKey, UserAgentNameValue>
        {
            public UserAgentName()
            {
            }
    
            public Hash32 Add([CanBeNull] string userAgentName)
            {
                string userAgentNameValue = (userAgentName ?? string.Empty);
                Hash32 result = Hash32.Compute(userAgentNameValue);
    
                UserAgentNameKey key = new UserAgentNameKey(result);
                UserAgentNameValue value = new UserAgentNameValue(userAgentNameValue);
    
                this.Add(key, value);
    
                return result;
            }
        }
    }

ディメンション テーブルの作成

大事な

ディメンション テーブルには、クラスと同じ名前を付ける必要があります。たとえば、UserAgentNameクラスには、クラスのプロパティに一致する列を持つ一致するdbo.UserAgentNameテーブルが必要です。

  • レポート データベースで、次の表を作成します。

    CREATE TABLE [dbo].[UserAgentName] (
            [UserAgentNameId] INT            NOT NULL,
            [UserAgentName]   NVARCHAR (1000) NOT NULL,
            CONSTRAINT [PK_UserAgentNames] PRIMARY KEY CLUSTERED ([UserAgentNameId] ASC)
    );
    
    GO

ファクト クラスとテーブルの作成

ディメンションテーブルには、そのユーザーエージェントによって生成された訪問の合計数と訪問の合計値に対するユーザーエージェントIDのリストが格納されます。

ファクトクラスの作成

  1. ファクト値クラスを作成します。

    namespace Documentation.Examples
    {
        using System;
        using Sitecore;
        using Sitecore.Analytics.Aggregation.Data.Model;
        using Sitecore.Analytics.Core;
        using Sitecore.Diagnostics;
    
        public class UserAgentValue : DictionaryValue
        {
            public long Visits { get; set; }
            public long Value { get; set; }
    
            //
            //   Reduces two <see cref="UserAgentValue"/> objects into one by
            //   adding corresponding members together.
            //
            [NotNull]
            internal static UserAgentValue Reduce([NotNull] UserAgentValue left, [NotNull] UserAgentValue right)
            {
                Debug.ArgumentNotNull(left, "left");
                Debug.ArgumentNotNull(right, "right");
                UserAgentValue result = new UserAgentValue();
                result.Visits = (left.Visits + right.Visits);
                result.Value = (left.Value + right.Value);
                return result;
            }
        }
    }
    手記

    Reduce() メソッドは、2つのファクト値を加算できるように実装する必要があります。

  2. ファクトキークラスを作成します。

    namespace Documentation.Examples
    {
        using System;
        using Sitecore;
        using Sitecore.Analytics.Aggregation.Data.Model;
        using Sitecore.Analytics.Core;
        using Sitecore.Diagnostics;
    
        public class UserAgentFactKey : DictionaryKey
        {
            public DateTime Date { get; set; }
            public Hash32 Checksum { get; set; }
            public Hash32 UserAgentNameId { get; private set; }
    
            public UserAgentFactKey(Hash32 userAgentHash32)
            {
                UserAgentNameId = userAgentHash32;
            }
        }
    }
  3. UserAgentFactKeyクラスとUserAgentValueクラスを使用するファクト クラスを作成します。

    namespace Documentation.Examples
    {
        using System;
        using Sitecore;
        using Sitecore.Analytics.Aggregation.Data.Model;
        using Sitecore.Analytics.Core;
        using Sitecore.Diagnostics;
    
        public class UserAgentFact : Fact<UserAgentFactKey, UserAgentValue>
        {
            public UserAgentFact() : base(UserAgentValue.Reduce)
            {
            }
        }
    }

ファクトテーブルの作成

ファクト テーブルには、クラスと同じ名前を付け、プレフィックスにFact_を付ける必要があります。たとえば、UserAgentFactファクト クラスには、クラスのプロパティに一致する列を持つ一致するdbo.Fact_UserAgentFactテーブルがあります。

  • レポート データベースで、次の表を作成します。

    CREATE TABLE [dbo].[Fact_UserAgentFact] (
            [Date]            SMALLDATETIME    NOT NULL,
            [Checksum]        INT              NOT NULL,
            [UserAgentNameId] INT              NOT NULL,
            [Visits]          BIGINT           NOT NULL,
            [Value]           BIGINT           NOT NULL,
            CONSTRAINT [FK_Fact_UserAgentFact_UserAgentName] FOREIGN KEY ([UserAgentNameId]) REFERENCES [dbo].[UserAgentName] ([UserAgentNameId]),
    );
    
    
    GO
    ALTER TABLE [dbo].[Fact_UserAgentFact] NOCHECK CONSTRAINT [FK_Fact_UserAgentFact_UserAgentName];
    
    
    GO
    CREATE NONCLUSTERED INDEX [IX_ByDateAndUserAgent]
            ON [dbo].[Fact_UserAgentFact]([Date] ASC, [UserAgentNameId] ASC)
            INCLUDE([Visits], [Value]);
    
    
    GO
    CREATE CLUSTERED INDEX [IX_ByDate]
            ON [dbo].[Fact_UserAgentFact]([Date] ASC, [Checksum] ASC);
    
    
    GO
    CREATE NONCLUSTERED INDEX [IX_Fact_UserAgentFact_UserAgent]
            ON [dbo].[Fact_UserAgentFact]([UserAgentNameId] ASC);

インタラクション集約プロセッサー・クラスの作成

InteractionAggregationPipelineProcessorを継承するクラスを作成します。次の例では、前の手順で作成したファクト クラスとディメンション クラスを使用します。プロセッサは、ファクト テーブルとディメンション テーブルを自動的に取り込みます。

namespace Documentation.Examples
{
    using System;
    using Sitecore;
    using Sitecore.Analytics.Aggregation;
    using Sitecore.Analytics.Aggregation.Data.Model;
    using Sitecore.Analytics.Aggregation.Pipeline;
    using Sitecore.Analytics.Core;
    using Sitecore.Diagnostics;

    public class UserAgentProcessor : InteractionAggregationPipelineProcessor
    {
        protected override void OnProcess(InteractionAggregationPipelineArgs args)
        {
            var useragent = args.Context.Interaction.UserAgent;

            if (string.IsNullOrEmpty(useragent))
            {
                return;
            }

            //
            // Create the fact key
            //
            Hash32 userAgentId = UpdateUserAgentDimension(useragent, args.Context.Results);
            UserAgentFactKey key = new UserAgentFactKey(userAgentId);
            key.Date = args.DateTimeStrategy.Translate(args.Context.Interaction.StartDateTime);
            key.Checksum = Hash32.Compute(userAgentId);

            //
            // Create the fact value.
            //
            UserAgentValue value = new UserAgentValue();
            value.Visits = 1;
            value.Value = args.Context.Interaction.EngagementValue;

            //
            // Emit fact.
            //
            UserAgentFact facts = args.Context.Results.GetFact<UserAgentFact>();
            facts.Emit(key, value);
        }
        protected static Hash32 UpdateUserAgentDimension(string useragent, IInteractionAggregationResults results)
        {
            Assert.ArgumentNotNull(useragent, nameof(useragent));
            Assert.ArgumentNotNull(results, nameof(results));

            UserAgentName dimension = results.GetDimension<UserAgentName>();
            Hash32 result = dimension.Add(useragent);

            return result;
        }
    }
}
手記

xConnect Client APIなどの ファセットやサービス には、インタラクション アグリゲーション プロセッサのコンテキストでアクセスできます。

構成にプロセッサを追加

プロセッサを次のように構成にパッチします。

<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:role="http://www.sitecore.net/xmlconfig/role/">
<sitecore role:require="Standalone or Processing">
    <pipelines>
    <group groupName="analytics.aggregation">
        <pipelines>
        <interactions>
            <processor type="Documentation.Examples.UserAgentProcessor, Documentation.Examples" />
        </interactions>
        </pipelines>
    </group>
    </pipelines>
</sitecore>
</configuration>
この記事を改善するための提案がある場合は、 お知らせください!