3

My task is to create a custom report which will export into a CSV that contains the details of all the users who have downloaded report files from our site. It will include not only information about the user, but information about the report they downloaded.

I have looked into creating an Outcome Definition within the Marketing Control Panel then registering the Contact Outcome via Tracker.Current.RegisterContactOutcome(outcome) when the user downloads the report.

I can see the outcome has been tracked in the XDB when I pull it up via RoboMongo but I have no clue on to retrieve that information. I tried using the OutcomeDefinitionManager to call the Get() method but am only able to find information about the Outcome Definition itself. Not necessarily about which contacts have registered to that Outcome.

Am I heading down the right path with using Outcome Definitions? If so, what method(s) do I need to call in order to retrieve such information about the user who registered to that Outcome?

1
  • You need to keep track also about what the user downloaded. The register outcome will only provide you the outcome. You can trigger a goal based on the document being downloaded and then trigger the outcome May 14, 2018 at 20:27

1 Answer 1

4

To retrieve Outcomes for given outcome definitions

There is no ready made API, but you can either write a Mongo query or extend Sitecore.Analytics.Outcome.Data.MongoDbOutcomeRepository.

public class CustomRepository : MongoDbOutcomeRepository
{
    public CustomRepository(MongoDbCollection mongoCollection) : base(mongoCollection)
    {
    }

    public CustomRepository(string connectionStringName) : base(connectionStringName)
    {
    }

    public CustomRepository(string connectionStringName, string collectionName) : base(connectionStringName, collectionName)
    {
    }

    public IList<IOutcome> GetOutcomesForDefinition(params ID[] definitionIds)
    {
      IMongoQuery query = Query.In("DefinitionId", definitionIds.Select(BsonValue.Create));
      MongoCursor<IOutcome> outcomes = this.MongoCollection.FindAs<IOutcome>(query);
      return outcomes.ToList();
    }
}

The repository can then be used as follows:

CustomRepository repo = new CustomRepository("analytics");
var contactOutcomes = repo.GetOutcomesForDefinition(definitionIds)
.Select(o => new {o.EntityId, o.DefinitionId}); //apply any projection needed

To retrieve Outcomes for a given contact

You need to use OutcomeManager:

// Instantiate the outcome manager.
var manager = Factory.CreateObject("outcome/outcomeManager", true) as OutcomeManager;

// Get all outcomes of a contact.
IReadOnlyCollection<IOutcome> outcomes = manager.GetForEntity<IOutcome>(contactId);​

There is a distinction between OutcomeDefinitionManager and OutcomeManager. The former is used to create, update, activate outcome definitions as you see them in Marketing Control Panel. The latter is capable of registering an outcome for a contact and retrieving registered outcomes.

3
  • But I want to get all the contacts who registered with a particular outcome definition. And how many times they registered with that outcome definition May 15, 2018 at 15:20
  • @RobertMichaelWatson, sorry, my bad. Completely misunderstood your question. I have updated my answer. Let me know if it helps.
    – grg
    May 15, 2018 at 20:14
  • Thanks! I was starting to look into the Sitecore code on how to do it and I kinda figured I had to go down this route. I'll let you know how it works out! May 15, 2018 at 20:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.