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 Interaction CreateOrGetInteraction(string hashtag, Contact contact, TwitterStatus tweet, Goal goal, out bool isNewInteraction) { Interaction interaction = null; isNewInteraction = false; using (var client = XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient()) { try { // Get interaction if it does exists interaction = GetInteraction(hashtag, tweet, contact); if (interaction != null) { return(interaction); } // Create if does not exists isNewInteraction = true; // Item ID of the "Twitter social community" Channel at // /sitecore/system/Marketing Control Panel/Taxonomies/Channel/Online/Social community/Twitter social community var channelId = Guid.Parse("{6D3D2374-AF56-44FE-B99A-20843B440B58}"); var userAgent = hashtag; interaction = new Interaction(contact, InteractionInitiator.Brand, channelId, userAgent); // Event - Page View var newEvent = new PageViewEvent(tweet.CreatedDate, Guid.Empty, 1, "en") { DataKey = tweet.IdStr, Text = tweet.Text, Url = "https://twitter.com/statuses/" + tweet.IdStr }; interaction.Events.Add(newEvent); // Event - Goal if (goal != null) { var newGoal = new XConnect.Goal(goal.ID.Guid, tweet.CreatedDate); interaction.Events.Add(newGoal); } client.AddInteraction(interaction); client.Submit(); return(interaction); } catch (XdbExecutionException ex) { // Manage exception Diagnostics.Log.Error( $"[HashTagMonitor] Error creating or retrieving interaction for contact '{contact.Personal().Nickname}' with hashtag '{hashtag}' and tweetId '{tweet.IdStr}'", ex, GetType()); } } return(interaction); }