Esempio n. 1
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);
                }
            }
        }
Esempio n. 2
0
        private bool SetFacet <T>(T data, string key, Contact contact, Sitecore.XConnect.Client.XConnectClient client) where T : Facet
        {
            try
            {
                var r = client.SetFacet <Facet>(contact, key, data);
                return(true);
            }
            catch (Exception e)
            {
            }

            return(false);
        }
Esempio n. 3
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);
            }
        }