Content impact calculation script

The impact value of a content item is automatically calculated using default metrics when a content item is published and added to publications.

If you enter a value in the Impact field, then the value you entered is used instead of the calculation, and you see this result:

RequestResponse
if (impactProperty.IsDirty) return;

var reaches = entity.GetPropertyValue <long>("Publication.Reach") * 5;
var clicks = entity.GetPropertyValue <long>("Publication.Clicks") * 10;
var shares = entity.GetPropertyValue <long>("Publication.Shares") * 50;
var likes = entity.GetPropertyValue <long>("Publication.Likes") * 20;
var comments = entity.GetPropertyValue <long>("Publication.Comments") * 30;

var impact = reaches + clicks + shares + likes + comments;
impactProperty.SetValue(impact);

However, you can modify the default weightings in the CMP - M.Usage - impact script. In this case, the impact is the sum of the weighted values.

In this example, the value of Clicks has been changed from 10 to 5, and the value of Likes has been changed from 20 to 5.

RequestResponse
using Newtonsoft.Json.Linq;

// Preferences
long reachFactor = 5;
long clicksFactor = 5;
long likesFactor = 5;
long commentsFactor = 30;
long sharesFactor = 50;

var usageId = Context.TargetId;
if (!usageId.HasValue) return;

MClient.Logger.Info("Calculating impact for entity " + usageId);

var usageLoadConfiguration = new EntityLoadConfiguration(
    CultureLoadOption.None,
    new PropertyLoadOption("Usage.Impact", "Usage.Metrics"),
    RelationLoadOption.None);

IEntity usageEntity = await MClient.Entities.GetAsync(usageId.Value, usageLoadConfiguration);
if (usageEntity == null) { MClient.Logger.Info($"Couldn't find entity with id: {usageId}"); return; }

var impactProperty = usageEntity.GetProperty<ICultureInsensitiveProperty>("Usage.Impact");
var metrics = usageEntity.GetPropertyValue<JToken>("Usage.Metrics");
if (metrics == null) { MClient.Logger.Info("Metrics property not found"); return; }

//Clicks
var clicks = (metrics.SelectToken("clicks")?.Value<long?>() ?? 0) * clicksFactor;

//Reach
var reaches = (
    metrics.SelectToken("reach")?.Value<long?>() ?? 0
    + metrics.SelectToken("connections")?.Value<long?>() ?? 0
) * reachFactor;

//Likes
var likes = (
    metrics.SelectToken("likes")?.Value<long?>() ?? 0
    + metrics.SelectToken("favorites")?.Value<long?>() ?? 0
    + metrics.SelectToken("+1s")?.Value<long?>() ?? 0
    + metrics.SelectToken("plusOnes")?.Value<long?>() ?? 0
) * likesFactor;

//Comments
var comments = (
    metrics.SelectToken("comments")?.Value<long?>() ?? 0
    + metrics.SelectToken("mentions")?.Value<long?>() ?? 0
    + metrics.SelectToken("replies")?.Value<long?>() ?? 0
) * commentsFactor;

//Shares
var shares = (
    metrics.SelectToken("shares")?.Value<long?>() ?? 0
    + metrics.SelectToken("retweets")?.Value<long?>() ?? 0
    + metrics.SelectToken("reshares")?.Value<long?>() ?? 0
    + metrics.SelectToken("repins")?.Value<long?>() ?? 0
) * sharesFactor;

var impact = reaches + clicks + shares + likes + comments;

if (impact == usageEntity.GetPropertyValue<long>("Usage.Impact"))
{
    MClient.Logger.Info("Nothing to update");
    return;
}

usageEntity.SetPropertyValue("Usage.Impact", impact);
await MClient.Entities.SaveAsync(usageEntity);

MClient.Logger.Info($"Recalculated impact for entity {usageEntity.Id}, new value: {impact}");

Clearing the cache and re-checking the impact produces the following figures:

Number of actions

Weighting

1 Reach

15

1 Click

5

1 Share

50

1 Like

5

1 Comment

30

Total

105

Do you have some feedback for us?

If you have suggestions for improving this article,