Showing posts with label Mongo. Show all posts
Showing posts with label Mongo. Show all posts

Thursday, February 18, 2016

Hedgehog Persona Tool Part III

Writing mock Sitecore Profile data as BSON documents with .NET driver for Mongo


I’ve spoken in previous posts about how useful Profiles and Pattern Cards can be and how profile data is handled by Mongo, and as I continued to work on the Persona Tool I saw a need to create and populate a mock collection of visit data. To accomplish that, I decided to work with .NET driver for Mongo and form my BSON documents on the back end. First things first: in my custom class responsible for communicating with Mongo I have a constructor that expects a connection string:

new AnalyticsDatabase(ConfigurationManager.ConnectionStrings["analytics"].ConnectionString);

In ConnectiongString.config file I have the following:

<add name="analytics" connectionString="mongodb://localhost:27001/my_analytics_db" />

I have references to MongoDB.Bson and MongoDB.Driver in my project. You can get the latest packages from NuGet at http://www.nuget.org/packages/MongoDB.Bson/, http://www.nuget.org/packages/MongoDB.Driver/.




My next step is to instantiate a MongoClient, then get the server and locate the database:

var client = new MongoClient(_connectionString);
var server = client.GetServer();
var database = server.GetDatabase("my_analytics_db");

I left the original Interactions collection intact and created a new one for my purposes.

var test = database.CreateCollection("Test");

As we know a Mongo collection is a collection of BSON documents. Therefore I created the following list:

List<BsonDocument> docs = new List<BsonDocument>();

Then I made a loop of however many fake visits I wanted and formed each document.
First I want to create the Values node for my document (highlighted in yellow below):




I pick a random value between 1 and 20 for the Count (which is a count of times someone in the real world would look at my content associated with any of the Profile Cards under the current Profile).

var count = rnd.Next(1, 20);

Then I create the values for the Values node:

var newValuesDoc = new BsonDocument {
{ "analytics", GetRandomFloat()},
{ "campaigns and targeting", GetRandomFloat() },
{ "content management", GetRandomFloat() },
{ "technology", GetRandomFloat() } };

Make sure that in your custom method that returns a random float you don't exceed the value: MaxValue on the Profile Key (i.e. "analytics") times Count. Please see my previous blog post that explains Count and Values in Mongo Profile data as well as how they relate to each other in detail.

Now that we have all our values we add them up and put them in the Total.

float total = 0;
foreach(BsonElement em in newValuesDoc.Elements)
{
       total += float.Parse(em.Value.ToString());
}

Next I created the Profile Node:

var myProfileNode = new BsonDocument {
{ "Count", count },
{ "Total", total },
{ "ProfileName", "Audience Segment" },
{ "Values", newValuesDoc }};

var profileDoc = new BsonDocument("Audience Segment", myProfileNode);

Give it the dates you desire and make the document that would represent a visit:

var doc = new BsonDocument{
{"_id", new Guid()},
{"t", "VisitData"},
{"ContactId", new Guid()},
{"StartDateTime", *give it some DateTime*},
{"EndDateTime", *give it some DateTime*},
{"SaveDateTime", *give it some DateTime*},
{"Profiles", profileDoc},
{"Value", rnd.Next(20, 100)} };

docs.Add(doc);

Then finally outside the loop insert the newly created batch of documents:

database.GetCollection<VisitData>("Test").InsertBatch(docs);

In a perfect world it’s best to work with real life visit data or use a tool like JMeter to generate visits to the site, but for putting together a POC or testing, mocking Profile data works.

With the code above users can create a legitimate Mongo collection containing Profile data. In my next blog post in this series I will cover some particularities of reading it using Mongo Query. If you'd like more information about persona development and marketing strategy, please reach out to Hedgehog Digital Marketing Innovation Team

Saturday, January 3, 2015

THINK BIG Sitecore 8 Mongo DB.

The time has come to start experimenting with Sitecore 8 and its powerful Analytics. Download is available here: https://dev.sitecore.net/en/Downloads/Sitecore_Experience_Platform/8_0/Sitecore_Experience_Platform_8_0.aspx

Since I chose to use SIM to install it I had to rename the zip file to "sitecore 8.0 rev. 141212"

I was particularly interested in Experience Profile where I can obtain a wealth of information about a specific visitor: their goals met, their score, value per visit, pages visited, etc, etc, etc. My assumption was that there would be some out of the box visitor information, at least a list of visitors. But the list was blank. The problem was that to use any Sitecore analytics I had to wire up the Mongo DB first.

Instructions I followed came from


There was a problem I encountered with installation.




Because I was only testing it on my local environment I could skip several steps in the process, such as moving the Mongo DB folder to another location or setting it up as a service. Setting up as a service allows automatic start of the Mongo DB when I power up my machine. But if you don't mind manually running the exe file then it's not an issue. The connection strings were set up automatically which was a bonus. 

The next step was to install MongoVue (http://www.mongovue.com/) It allows visualization into the database after you add a connection. 


I set a simple goal (home page visit event) and went to the site in the browser. The next step is going to Sitecore dashboard and click on Experience Profile. From there you can navigate to any visitor (provided there are some).



The overview tab on a visitor detail page is where achieved goals are listed (under Latest Events). 



One more thing. I needed set the session timeout to 1 min instead of the default 20 minutes in the web config file. Otherwise I'd have to wait for 20 minutes for analytics data to be reflected in the profile. 

<sesstionState mode="InProc" cookieless="false" timeout="1">