Skip to content
Permalink
Browse files

DCN-111 - Optimizely personalization based on target audiences

  • Loading branch information...
kenticomartinh committed Mar 14, 2019
1 parent 1257368 commit d37e938ee42b504d073cadba8651f97f9f31a67d
Showing with 69 additions and 3 deletions.
  1. +1 −0 public/index.html
  2. +5 −2 src/Components/LatestArticles.js
  3. +3 −0 src/Models/article.js
  4. +2 −1 src/Stores/Article.js
  5. +58 −0 src/Utilities/Optimizely.js
@@ -6,6 +6,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta charset="UTF-8" />
<title>Dancing Goat</title>
<script src="https://cdn.optimizely.com/js/13645900009.js"></script>
</head>

<body>
@@ -2,15 +2,18 @@ import React, { Component } from 'react';
import { ArticleStore } from '../Stores/Article';
import dateFormat from 'dateformat';
import { translate } from 'react-translate';

import { dateFormats } from '../Utilities/LanguageCodes';
import Link from '../Components/LowerCaseUrlLink';
import { getPersonalizedArticles } from '../Utilities/Optimizely';

const articleCount = 5;

let getState = props => {
return {
articles: ArticleStore.getArticles(articleCount, props.language)
articles: getPersonalizedArticles(
ArticleStore.getArticles(100, props.language),
articleCount
)
};
};

@@ -56,6 +56,9 @@ export class Article extends ContentItem {
if (fieldName === 'url_pattern') {
return 'urlPattern';
}
if (fieldName === 'target_audiences') {
return 'targetAudiences';
}
return fieldName;
},
linkResolver: link => resolveContentLink(link)
@@ -49,7 +49,8 @@ class Article {
'metadata__twitter_site',
'metadata__twitter_creator',
'metadata__twitter_description',
'metadata__twitter_image'
'metadata__twitter_image',
'target_audiences'
]);

if (language) {
@@ -0,0 +1,58 @@
function getActiveAudiences() {
const audienceIds = [];
const experiments = window.optimizely.get('state').getExperimentStates();
for (var experimentId in experiments) {
if (experiments.hasOwnProperty(experimentId)) {
const experiment = experiments[experimentId];
for (var i = 0; i < experiment.audiences.length; i++) {
const audienceId = experiment.audiences[i].id;
if (audienceIds.indexOf(audienceId) < 0) {
audienceIds.push(audienceId);
}
}
}
}

return audienceIds;
}

function isArticleForAudiences(article, audienceIds) {
const targetAudiences =
article.targetAudiences &&
article.targetAudiences.value &&
JSON.parse(article.targetAudiences.value);
const targetAudienceIds =
targetAudiences && targetAudiences.map(item => `${item.id}`);
if (!targetAudienceIds) {
// False for no audiences when none set, true for all audiences when not set
return false;
}

for (var i = 0; i < targetAudienceIds.length; i++) {
if (audienceIds.indexOf(targetAudienceIds[i]) >= 0) {
return true;
}
}

return false;
}

export function getPersonalizedArticles(articles, count) {
const audienceIds = getActiveAudiences();
if (!audienceIds.length) {
return articles.slice(0, count);
}

const matching = [];
for (var i = 0; i < articles.length; i++) {
const article = articles[i];
if (isArticleForAudiences(article, audienceIds)) {
matching.push(article);
if (matching.length >= count) {
break;
}
}
}

return matching;
}

0 comments on commit d37e938

Please sign in to comment.
You can’t perform that action at this time.