dedupe

Description

Remove duplicate items from an array of content recommendations, so that the same product or article is not displayed more than once.

array dedupe( array input [, string field] )

Based on the specified field–the name of any content attribute supported by the Content API such as a key (e.g. url) or var–deduplicate the array of content items, returning a new content array in which the value for each field exists only once.

The default field (when the field parameter is not specified) is url — the URL of the content items.

Note: If you want to deduplicate a Sailthru-generated content feed while maintaining the locations of content items that are pinned using Recommendation Manager, use filter_content() instead.

Examples

  • Deduplicate the content array based on matches of the url field:
    {content = dedupe(content)} or
    {content = dedupe(content, "url")}
    Either of the above would transform the following content array to remove duplicate URLs:

    content Before

    {
        "url" : "http://example.com/1",
        <additional fields>
    },
    {
        "url" : "http://example.com/2",
        <additional fields>
    },
    {
        "url" : "http://example.com/2",
        <additional fields>
    },
    {
        "url" : "http://example.com/3",
        <additional fields>
    }
    content After

    {
        "url" : "http://example.com/1",
        <additional fields>
    },
    {
        "url" : "http://example.com/2",
        <additional fields>
    },
    {
        "url" : "http://example.com/3",
        <additional fields>
    }
  • Deduplicate the content array based on matches of the sku field:
    {content = dedupe(content, "sku")}
  • Deduplicate the content array based on matches of the title field:
    {content = dedupe(content, "title")}
  • Deduplicate the content array based on matches of a content var named item_id:
    {content = dedupe(content, "vars.item_id")}
  • Deduplicate the content array based on matches of a content var named item.id:
    {content = dedupe(content, "vars.item.id")}

Display Content with Vars Matching Users’ Interest/Subscription Variables

Use Case: You have individual categories users can follow, each denoted by their own custom field, and on a data feed, you want to match this user field against a “sailthru.category” content variable in order to create a personalized bucket of content for users. You also want a backup section in case someone isn’t following a certain category.

Zephyr:

On the User Profile:

zephyr example tech media news number 1

In the Setup:

{*Creating an assignment called userContent, which will go through each "category" content var, find if you have that same user var with the value of 1, and add it to a "userContent" array*}{userContent = filter_content(content, lambda c: profile.vars[c.vars.sailthru_category] == 1)}{*Personalize the order in which industries appear based on interest tags, pulling the top 3*}{personalizedContent = personalize({
   "content" : userContent,
   "algorithm" : "interest",
   "size" : 3
})}

In the Code:

{if length(userContent) > 0}
<p>Based on your preferences, here are stories you'll enjoy:</p>
{foreach personalizedContent as c}
  <p>From the {c.vars.sailthru_category} category: <a href={c.url}>{c.title}</a></p>
  {/foreach}
{else}
Want personalized recommendations? Tell us what you like <a href="http://example.com/preferences">here</a> to start following a certain category!</p>
{/if}

Output:

(Based on the above user profile)

Here are stories from your selected categories:

From the news category: Spider-Man: Threat or Menace?
From the tech category: Get Your First Look at the New iPhone at the Times Square Apple Store
From the news category: Alexander Harris elected to Los Angeles House of Representatives

(If nothing matches)

Want personalized recommendations? Tell us what you like here to start following a certain category!

Explanation:

This script uses the filter_content() function to iterate through each user profile variable to see if each user has a variable on their profile that matches the value of a content variable on a data feed. For instance, in the Media feed, the values for each sailthru.category custom variable are: media, tech, news, news, and fashion. On the example user profile, the user has “tech”, “media”, and “news” are custom fields that equal “1” (i.e. they are “subscribed” to those categories). Within the filter_content() script, the value of the profile.var (aka custom field) dynamically populates as it iterates through each item in the feed. For instance, for the first item in the Media feed (“Stephen Kings’s New Book…”), the sailthru.category value is “media”. The script check each user profile to see if “profile.vars.media == 1″. If it does, that piece of content is added to a “userContent” variable, which is an array of items from the feed. If not, the item is skipped. For this particular user, since they’re following each category represented in the feed besides “fashion”, only the last article is skipped.

Next, a local variable called “personalizedContent” is created, which uses the personalize() function to pick the top three stories from the “userContent” object based on individual user interest. Finally, in the Code, an “if” statement checks if there’s more than 0 pieces of content in that userContent object (i.e. there’s an item in the feed that matches a category a user is subscribed to). If so, display the category, title, and URL. If not, show a prompt encouraging users to update their preferences and choose categories to follow.

Top