public void ProcessHashTag(string hashtag, PatternCard patternCard, Goal goal) { // Get all tweets from hashtag var tweets = _twitter.GetTweets(hashtag, TwitterSearchResultType.Recent, 200); foreach (var tweet in tweets) { // Create Contact + Identifier + Personal Info var contact = CreateOrGetContact(hashtag, tweet); if (contact == null) { continue; } // Create Interaction var isNewInteraction = false; var interaction = CreateOrGetInteraction(hashtag, contact, tweet, goal, out isNewInteraction); if (interaction == null) { continue; } // Apply Pattern Card to the Contact if the Interaction is new if (isNewInteraction && patternCard != null) { ApplyPatternCard(contact, tweet, patternCard); } } }
private void ApplyPatternCard(Contact contact, TwitterStatus tweet, PatternCard patternCard) { using (var client = XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient()) { try { // Get updated contact with ContactBehaviorProfile facet var updatedContact = client.Get(new IdentifiedContactReference("twitter", tweet.User.ScreenName), new ContactExpandOptions(ContactBehaviorProfile.DefaultFacetKey)); if (updatedContact == null) { return; } // Retrieve facet or create new var isNewFacet = false; var facet = updatedContact.GetFacet <ContactBehaviorProfile>(ContactBehaviorProfile.DefaultFacetKey); if (facet == null) { isNewFacet = true; facet = new ContactBehaviorProfile(); } // Change facet properties var score = new ProfileScore { ProfileDefinitionId = patternCard.GetProfile().ID.ToGuid() }; if (score.Values == null) { score.Values = new Dictionary <Guid, double>(); } var patterns = patternCard.GetPatterns(); foreach (var pair in patterns) { score.Values.Add(pair.Key, pair.Value); } if (facet.Scores.ContainsKey(patternCard.GetProfile().ID.Guid)) { facet.Scores[patternCard.GetProfile().ID.Guid] = score; } else { facet.Scores.Add(patternCard.GetProfile().ID.Guid, score); } // Save the facet if (isNewFacet) { client.SetFacet <ContactBehaviorProfile>(updatedContact, ContactBehaviorProfile.DefaultFacetKey, facet); } else { client.SetFacet(updatedContact, ContactBehaviorProfile.DefaultFacetKey, facet); } client.Submit(); } catch (XdbExecutionException ex) { Diagnostics.Log.Error( $"[HashTagMonitor] Error applying Patter Card to Contact '{contact.Personal().Nickname}'", ex, GetType()); } } }