Thursday, November 15, 2018

Connecting tracker Contact with xConnect contact

using System;
using System.Web.Mvc;
using Feature.Salesforce.Models;
using Sitecore.XConnect;
using Sitecore.XConnect.Client;
using Feature.Salesforce.Repositories;
using Xcentium.SitecoreAsAService.Feature.Salesforce.Models;
using System.Collections.Generic;
using Sitecore.Analytics;
using Sitecore.Analytics.Data;
using System.Linq;
using System.Xml;
using Newtonsoft.Json;
using Sitecore.XConnect.Client.Serialization;
using Sitecore.XConnect.Client.Configuration;
using Sitecore.Analytics.Model;


Cookie name with contact ID: SC_ANALYTICS_GLOBAL_COOKIE


 //Check if tracker is active or not
                if (!Sitecore.Analytics.Tracker.IsActive)
                {
                    Sitecore.Analytics.Tracker.StartTracking();
                }

1. Trigger a goal for Current Tracker Contact


Sitecore.Data.Items.Item goalItem = Sitecore.Context.Database.GetItem("{968897F1-328A-489D-88E8-BE78F4370958}");
 var goalTrigger = Sitecore.Analytics.Tracker.MarketingDefinitions.Goals[goalItem.ID.ToGuid()];
                        var goalEventData = Sitecore.Analytics.Tracker.Current.CurrentPage.RegisterGoal(goalTrigger);
                        goalEventData.Data = goalItem["Name"];
                        goalEventData.ItemId = goalItem.ID.ToGuid();
                        goalEventData.DataKey = goalItem.Paths.Path;
                        goalEventData.Text = "Goal for a phone call with Salesforce Representative";




2. Tie current contact with a contact whose ID you got from query string (from the analytics cookie)

using (XConnectClient xclient = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
                        {

 var trackerIdentifier = new IdentifiedContactReference(
                                        Sitecore.Analytics.XConnect.DataAccess.Constants.IdentifierSource,
                                        Sitecore.Analytics.Tracker.Current.Contact.ContactId.ToString("N"));

                                    var xConnectContact = client.Get<Contact>(trackerIdentifier,
                                        new Sitecore.XConnect.ContactExpandOptions());

                                    //if (contact == null)
                                    //{
                                    //    manager.CreateContact(ID.NewID);
                                    //}

                                    if (Sitecore.Analytics.Tracker.Current.Contact.IsNew)
                                    {
                                        Sitecore.Analytics.Tracker.Current.Contact.ContactSaveMode = ContactSaveMode.AlwaysSave;
                                        //manager.SaveContactToCollectionDb(Sitecore.Analytics.Tracker.Current.Contact);
                                    }




}


3. Trigger a goal for xConnect contact

//add interaction
                                Guid channelId = Guid.Parse("86c7467a-d019-460d-9fa9-85d6d5d77fc4"); // Replace with real channel ID GUID
                                string userAgent = "Sample User Agent";
                                var interaction = new Interaction(xConnectContact, InteractionInitiator.Brand, channelId, userAgent);

                                // Get goal ID - this is the ID of the item in Sitecore
                                var fakeGoalId = Guid.Parse("{968897F1-328A-489D-88E8-BE78F4370958}".ToLower());

                                // Create new instance of goal
                                var goal = new Goal(fakeGoalId, DateTime.UtcNow);
                                {
                                };

                                //trigger goal for existing contact                       
                                interaction.Events.Add(goal);

                                // Add interaction operation to client
                                xclient.AddInteraction(interaction);

                                try
                                {
                                    // Submit interaction
                                    xclient.Submit();
                                }
                                catch (Exception ex)
                                {
                                    return Json(new
                                    {
                                        result = "Error submitting an interaction"
                                    }, JsonRequestBehavior.AllowGet);
                                }





No comments:

Post a Comment