private void CreateInteraction(Contact contact, Guid goalId, string goalData)
        {
            using (Sitecore.XConnect.Client.XConnectClient client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
            {
                try
                {
                    var newContact = new Sitecore.XConnect.Contact();
                    client.AddContact(newContact);

                    // Create new interaction for this contact
                    Guid   channelId   = Guid.NewGuid(); // Replace with channel ID from Sitecore
                    string userAgent   = "Mozilla/5.0 (Nintendo Switch; ShareApplet) AppleWebKit/601.6 (KHTML, like Gecko) NF/4.0.0.5.9 NintendoBrowser/5.1.0.13341";
                    var    interaction = new Sitecore.XConnect.Interaction(newContact, InteractionInitiator.Brand, channelId, userAgent);


                    // Create new instance of goal
                    Sitecore.XConnect.Goal goal = new Goal(goalId, DateTime.UtcNow);
                    {
                    };
                    goal.Data = goalData;
                    // Add goal to interaction
                    interaction.Events.Add(goal);

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

                    // Submit interaction
                    client.Submit();
                }
                catch (Exception ex)
                {
                    // Handle exception
                }
            }
        }
Exemplo n.º 2
0
        public void RecordAnalytics(string[] contactDetails, string channel = null, string goalId = null, string chatId = null, string questionId = null, string answerId = null)
        {
            using (Sitecore.XConnect.Client.XConnectClient client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
            {
                try
                {
                    Contact contact = null, existingContact = null;
                    //// Retrieve contact

                    existingContact = client.Get <Contact>(new IdentifiedContactReference(contactDetails[0], contactDetails[1]), new ContactExpandOptions());

                    if (existingContact != null)
                    {
                        contact = existingContact;
                    }
                    else
                    {
                        contact = new Contact(new ContactIdentifier(contactDetails[0], contactDetails[1], ContactIdentifierType.Known));
                        client.AddContact(contact);
                    }
                    if (contact != null)
                    {
                        var result = getInteractionFacets(contact.Id.GetValueOrDefault(), chatId, questionId, answerId);
                        if (!result && !String.IsNullOrEmpty(answerId))
                        {
                            var    channelId = Guid.Parse(channel);
                            string userAgent = HttpContext.Current.Request.UserAgent;

                            Interaction interaction = new Interaction(contact, InteractionInitiator.Brand, channelId, userAgent);

                            ChatBotAnalytics CBData = new ChatBotAnalytics()
                            {
                                ChatId   = chatId,
                                Question = questionId,
                                Answer   = answerId
                            };

                            client.SetFacet <ChatBotAnalytics>(interaction, ChatBotAnalytics.DefaultFacetKey, CBData);

                            var offlineGoal   = Guid.Parse(goalId);
                            var xConnectEvent = new Goal(offlineGoal, DateTime.UtcNow);

                            interaction.Events.Add(xConnectEvent);
                            client.AddInteraction(interaction);

                            client.Submit();
                        }
                    }
                }
                catch (Exception ex)
                {
                    Log.Info(ex.ToString(), this);
                }
            }
        }
Exemplo n.º 3
0
        public void RegisterGoal(Guid channelID, Guid goalID, string userAgent)
        {
            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";

                    //string globalCookie = Request.Cookies["SC_ANALYTICS_GLOBAL_COOKIE"].Value;
                    //var reference = new IdentifiedContactReference(Sitecore.XConnect.Constants.AliasIdentifierSource, globalCookie.Split('|')[0]);

                    string trackerID = Sitecore.Analytics.Tracker.Current.Contact.ContactId.ToString();

                    //var reference = new IdentifiedContactReference(Sitecore.XConnect.Constants.AliasIdentifierSource,trackerID);
                    var reference = new IdentifiedContactReference("xDB.Tracker", trackerID);

                    Sitecore.XConnect.Contact contact = client.Get <Sitecore.XConnect.Contact>(reference, new ContactExpandOptions()
                    {
                    });

                    if (contact == null)
                    {
                        contact = new Sitecore.XConnect.Contact();
                    }

                    var interaction = new Sitecore.XConnect.Interaction(contact, InteractionInitiator.Brand, channelID, userAgent);

                    // Guid goalID = Guid.Parse("{9B6DC6A2-EB41-47B5-B10F-39AC37DE214F}"); // ID of goal item

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

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

                    interaction.Events.Add(goal);



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

                    client.Submit();
                }
                catch (XdbExecutionException ex)
                {
                    // Handle exception
                }
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// This method adds the contact details in xdb - right now we are passing hardcoded data.
        /// </summary>
        public void AddContactDetails()
        {
            using (Sitecore.XConnect.Client.XConnectClient client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
            {
                try
                {
                    // New anonymous contact
                    //test
                    var newContact = new Sitecore.XConnect.Contact();
                    client.AddContact(newContact);

                    Guid   channelId = Guid.Parse("86c7467a-d019-460d-9fa9-85d6d5d77fc4"); // Replace with real channel ID GUID
                    string userAgent = "Astha Prasad";

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

                    var fakeItemID = Guid.Parse("5746c4f3-7e16-40d9-ba1d-14c70875724c"); // Replace with real item ID

                    Sitecore.XConnect.Collection.Model.PageViewEvent pageView = new PageViewEvent(DateTime.UtcNow, fakeItemID, 3, "en")
                    {
                        Duration = new TimeSpan(0, 0, 30),
                        Url      = "/test/url/test/url?query=testing"
                    };

                    interaction.Events.Add(pageView);

                    client.AddInteraction(interaction);

                    client.Submit();
                }
                catch (XdbExecutionException ex)
                {
                    // Handle exception
                }
            }
        }
Exemplo n.º 5
0
        public void UpdateContact()
        {
            try
            {
                using (Stream receiveStream = Request.InputStream)
                {
                    using (StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8))
                    {
                        var contactToUpdateRequest = readStream.ReadToEnd();
                        var jobj       = JObject.Parse(contactToUpdateRequest);
                        var properties = (JObject)jobj.Property("properties").Value;
                        if (properties != null)
                        {
                            var hubspotConfigItemId = Settings.GetSetting(ConfigItemId);
                            if (string.IsNullOrEmpty(hubspotConfigItemId))
                            {
                                hubspotConfigItemId = "";
                            }

                            var hubspotConfigItem = Sitecore.Context.Database.GetItem(hubspotConfigItemId);
                            var emailFieldName    = hubspotConfigItem[Templates.HubspotFormConfig.Fields.EmailFieldName];
                            // get contact from sitecore database by email from request
                            var userEmail = ((JProperty)properties[emailFieldName].First).Value.Value <string>();
                            if (!string.IsNullOrEmpty(userEmail))
                            {
                                var contactToUpdate = this.GetContact(userEmail);
                                if (contactToUpdate != null)
                                {
                                    var leadScores = hubspotConfigItem[Templates.HubspotFormConfig.Fields.LeadScores];
                                    if (!string.IsNullOrEmpty(leadScores))
                                    {
                                        NameValueCollection parsedLeadScoresList = Sitecore.Web.WebUtil.ParseUrlParameters(leadScores);
                                        string[]            leadScoresList       = null;
                                        if (parsedLeadScoresList != null)
                                        {
                                            leadScoresList = parsedLeadScoresList.AllKeys;
                                        }
                                        var maxScoreValue = int.MinValue;
                                        var maxScoreName  = string.Empty;
                                        using (Sitecore.XConnect.Client.XConnectClient client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
                                        {
                                            foreach (var leadScore in leadScoresList)
                                            {
                                                // checking which it's the biggest score/leadScore value to update the custom score facet in sitecore
                                                if (properties[leadScore] != null)
                                                {
                                                    try
                                                    {
                                                        var leadScoreScoreValue = ((JProperty)properties[leadScore].First).Value.Value <int>();
                                                        // checking the biggest leadScore/score value
                                                        if (leadScoreScoreValue > maxScoreValue)
                                                        {
                                                            maxScoreValue = leadScoreScoreValue;
                                                            maxScoreName  = parsedLeadScoresList[leadScore];
                                                        }
                                                    }
                                                    catch (Exception ex)
                                                    {
                                                        Sitecore.Diagnostics.Log.Error("Error casting leadScore score value for " + leadScore + ex.ToString(), this);
                                                    }
                                                }
                                            }

                                            var contactScoreFacet = contactToUpdate.GetFacet <ScoreFacet>(ScoreFacet.DefaultFacetKey);
                                            if (contactScoreFacet != null)
                                            {
                                                contactScoreFacet.HubspotScore = maxScoreValue;
                                                client.SetFacet(contactToUpdate, ScoreFacet.DefaultFacetKey, contactScoreFacet);
                                            }
                                            else
                                            {
                                                client.SetFacet(contactToUpdate, ScoreFacet.DefaultFacetKey, new ScoreFacet()
                                                {
                                                    HubspotScore = maxScoreValue
                                                });
                                            }

                                            var contactScoreNameFacet = contactToUpdate.GetFacet <ScoreNameFacet>(ScoreNameFacet.DefaultFacetKey);
                                            if (contactScoreNameFacet != null)
                                            {
                                                contactScoreNameFacet.HubspotScoreName = maxScoreName;
                                                client.SetFacet(contactToUpdate, ScoreNameFacet.DefaultFacetKey, contactScoreNameFacet);
                                            }
                                            else
                                            {
                                                client.SetFacet(contactToUpdate, ScoreNameFacet.DefaultFacetKey, new ScoreNameFacet()
                                                {
                                                    HubspotScoreName = maxScoreName
                                                });
                                            }

                                            // Submit operations as batch
                                            client.Submit();
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Sitecore.Diagnostics.Log.Error("Error during update of contact: ", ex, this);
            }
        }