August 08, 2015

Profiling and Personalization using Target Groups in DXA DD4T

Profiling and Personalization aka P&P is very useful feature of Tridion. User's behavior can be tracked and user can be targeted with set of content.

In this post i am going to detail P&P custom characteristics use with DXA and DD4T. We started with SmartTarget evaluation but due to license cost and hardware requirement for Fredhopper caused us to evaluate this Out of the box feature of Tridion to use with DXA.

P&P provides basic but very useful personalization features. 
  1. Tracking Keys
  2. Custom Characteristics
Tracking Keys: Tracking keys are used for Implicit Personalization. Using these  you can can track interest as user visits various content. This functionality works on the tridion Category/keywords. Editor add the "Activate tracking" tbb  in the page template and add the category for tracking in CT's Tracked Category.  Schema should also be added to the component also. publish the page.
On your presentation side custom code has to be written using WAI to track the activities. 

Custom Characteristics:  CC are  used for Explicit Personalization. User information for targeting can be collected using some form e.g. gender, age, city, country. This information can be persisted in the broker db for further use.

Steps to use CC

Page Template Change

Include dd4t tbb "Add Target Groups" on the page template.

Define TargetGroup : 
  • Right click on folder and select  TargetGroup.
  • Set the characteristics as shown below.














  • TargetGroup can only be assigned to component Presentations. Lets do it & Publish the page.









XML: 
After publishing page, DD4T page xml will contain Condition need under Component-presentation node.

<Page>
.
.
 <ComponentPresentation>
.
<Conditions>
        <Condition d5p1:type="CustomerCharacteristicCondition" xmlns:d5p1="http://www.w3.org/2001/XMLSchema-instance">
          <Negate>false</Negate>
          <Name>city</Name>
          <Operator>Contains</Operator>
          <Value xmlns:q3="http://www.w3.org/2001/XMLSchema" d5p1:type="q3:string">Mumbai</Value>
        </Condition>
        <Condition d5p1:type="CustomerCharacteristicCondition" xmlns:d5p1="http://www.w3.org/2001/XMLSchema-instance">
          <Negate>false</Negate>
          <Name>city</Name>
          <Operator>Contains</Operator>
          <Value xmlns:q4="http://www.w3.org/2001/XMLSchema" d5p1:type="q4:string">Delhi</Value>
        </Condition>
      </Conditions>
    </ComponentPresentation>
.
.
</Page>

Presentation server

Here XML is de-serialized to c# object
Note: Current DD4T 1.31 which is part of latest  DXA has bug , because of that XML-with- -conditions-tag de-serialization fails.As this is fixed in DD4T 2.0 so i used some code from 2.0 version and fixed it.

When user access the website where we have WAI module configured
  • Cookie is generated with USER_ID key. 
  • If you have non-login website this USER_ID is generated automatically by WAI
  • You can write custom logic to update the USER_ID with your website user_id
There is no OOTB code to work on Conditions on DXA/DD4T, so you have to write custom code to remove the component presentation where condition fails.

Example user is not from Delhi or Mumbai. then delete that component presentation. 

 var  waiPage= new WAIPage(page.Id, this.HttpContext.ApplicationInstance.Context);
            CustomerCharacteristics cc= new CustomerCharacteristics(waiPage.User);
            var city= customerCharacteristics.GetValue("city");
            if (string.IsNullOrEmpty(city))
            {
                cc.SetValue("country", "Kolkata");//this city may come from your Geolocation module.

                cc.ExecuteUpdate();
            }                                                                       
           foreach(var cp in page)
            {
                foreach(var condition in cp.Conditions)
                {
                    if (condition is CustomerCharacteristicCondition)
                    {
                        
                        var ccCondition= (CustomerCharacteristicCondition)condition;
                        var node = (XmlNode[])ccCondition.Value;
                        string city= node[2].Value;       
                                    }
//now you can remove the component presentation here whereever conditions does not match.

                }
            }

now updated page is processed further via DXA for generation of views