Skip to content

Commit

Permalink
upgrade to @sitecore-cloudsdk/personalize / events 0.3.1 and add pass…
Browse files Browse the repository at this point in the history
…ing of variantIds to personalize call from middleware
  • Loading branch information
ambrauer committed Jul 23, 2024
1 parent 5f2662e commit 8482883
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 44 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"dependencies": {
"@sitecore/components": "^1.1.10",
"@sitecore-cloudsdk/events": "^0.3.0",
"@sitecore-cloudsdk/events": "^0.3.1",
"@sitecore-feaas/clientside": "^0.5.17"
}
}
6 changes: 3 additions & 3 deletions packages/sitecore-jss-nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"url": "https://github.com/sitecore/jss/issues"
},
"devDependencies": {
"@sitecore-cloudsdk/personalize": "^0.3.0",
"@sitecore-cloudsdk/personalize": "^0.3.1",
"@types/chai": "^4.3.4",
"@types/chai-as-promised": "^7.1.5",
"@types/chai-string": "^1.4.2",
Expand Down Expand Up @@ -65,8 +65,8 @@
"typescript": "~4.9.4"
},
"peerDependencies": {
"@sitecore-cloudsdk/events": "^0.3.0",
"@sitecore-cloudsdk/personalize": "^0.3.0",
"@sitecore-cloudsdk/events": "^0.3.1",
"@sitecore-cloudsdk/personalize": "^0.3.1",
"next": "^14.1.0",
"react": "^18.2.0",
"react-dom": "^18.2.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -880,19 +880,33 @@ describe('PersonalizeMiddleware', () => {
const personalizeStub = sinon.stub();
personalizeStub
.withArgs(
sinon.match({ friendlyId: CdpHelper.getComponentFriendlyId(pageId, 'component1', 'en') }),
sinon.match({
friendlyId: CdpHelper.getComponentFriendlyId(pageId, 'component1', 'en'),
variantIds: ['component1_default', 'component1_variant1'],
}),
sinon.match.any
)
.returns(Promise.resolve({ variantId: 'component1_default' }));
personalizeStub
.withArgs(
sinon.match({ friendlyId: CdpHelper.getComponentFriendlyId(pageId, 'component2', 'en') }),
sinon.match({
friendlyId: CdpHelper.getComponentFriendlyId(pageId, 'component2', 'en'),
variantIds: ['component2_default', 'component2_variant1', 'component2_variant2'],
}),
sinon.match.any
)
.returns(Promise.resolve({ variantId: 'component2_variant1' }));
personalizeStub
.withArgs(
sinon.match({ friendlyId: CdpHelper.getComponentFriendlyId(pageId, 'component3', 'en') }),
sinon.match({
friendlyId: CdpHelper.getComponentFriendlyId(pageId, 'component3', 'en'),
variantIds: [
'component3_default',
'component3_variant1',
'component3_variant2',
'component3_variant3',
],
}),
sinon.match.any
)
.returns(Promise.resolve({ variantId: 'component3_variant3' }));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,25 +133,30 @@ export class PersonalizeMiddleware extends MiddlewareBase {
friendlyId,
language,
timeout,
variantIds,
}: {
params: ExperienceParams;
friendlyId: string;
language: string;
timeout?: number;
variantIds?: string[];
},
request: NextRequest
) {
const personalizationData = {
channel: this.config.cdpConfig.channel || 'WEB',
currency: this.config.cdpConfig.currency ?? 'USD',
friendlyId,
params,
language,
};

debug.personalize('executing experience for %s %o', friendlyId, params);

return (await personalize(request, personalizationData, { timeout })) as {
return (await personalize(
request,
{
channel: this.config.cdpConfig.channel || 'WEB',
currency: this.config.cdpConfig.currency ?? 'USD',
friendlyId,
params,
language,
pageVariantIds: variantIds,
},
{ timeout }
)) as {
variantId: string;
};
}
Expand Down Expand Up @@ -309,18 +314,25 @@ export class PersonalizeMiddleware extends MiddlewareBase {

await Promise.all(
executions.map((execution) =>
this.personalize({ friendlyId: execution.friendlyId, params, language, timeout }, req).then(
(personalization) => {
const variantId = personalization.variantId;
if (variantId) {
if (!execution.variantIds.includes(variantId)) {
debug.personalize('invalid variant %s', variantId);
} else {
identifiedVariantIds.push(variantId);
}
this.personalize(
{
friendlyId: execution.friendlyId,
variantIds: execution.variantIds,
params,
language,
timeout,
},
req
).then((personalization) => {
const variantId = personalization.variantId;
if (variantId) {
if (!execution.variantIds.includes(variantId)) {
debug.personalize('invalid variant %s', variantId);
} else {
identifiedVariantIds.push(variantId);
}
}
)
})
)
);

Expand Down
36 changes: 18 additions & 18 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5997,30 +5997,30 @@ __metadata:
languageName: node
linkType: hard

"@sitecore-cloudsdk/core@npm:^0.3.0":
version: 0.3.0
resolution: "@sitecore-cloudsdk/core@npm:0.3.0"
"@sitecore-cloudsdk/core@npm:^0.3.1":
version: 0.3.1
resolution: "@sitecore-cloudsdk/core@npm:0.3.1"
dependencies:
"@sitecore-cloudsdk/utils": ^0.3.0
"@sitecore-cloudsdk/utils": ^0.3.1
debug: ^4.3.4
checksum: f494bd1d779204cc4ad9c5ae0692a5897ba1b391637a4b40e888363ad00c94feb0a9c5e57c4f67977aacd1905da29edf0fef35d1e8f7e61f7cac7385b08dc029
checksum: c19d750b1c194f7575d20b3d46739d7b41545087a9fb794ba44ab9b2d812f2b2035caf19c4f4ba51e6ff628c99f2e4a18b0a7a2578b413c7adb04f3d77f392a3
languageName: node
linkType: hard

"@sitecore-cloudsdk/personalize@npm:^0.3.0":
version: 0.3.0
resolution: "@sitecore-cloudsdk/personalize@npm:0.3.0"
"@sitecore-cloudsdk/personalize@npm:^0.3.1":
version: 0.3.1
resolution: "@sitecore-cloudsdk/personalize@npm:0.3.1"
dependencies:
"@sitecore-cloudsdk/core": ^0.3.0
"@sitecore-cloudsdk/utils": ^0.3.0
checksum: d7132692a896198b25603db6fb8d3e7813b55389b276c9433e67033f990c06ed818a0979f52a8d0af2492de42d09c1b4fe28c4d4c18a8eca6fa0d82fe7e99d43
"@sitecore-cloudsdk/core": ^0.3.1
"@sitecore-cloudsdk/utils": ^0.3.1
checksum: e48b1429f67be22069bfd8cc6349660ed346877fef5537def5f36c962fd2264969ff02fb2e16333dabc683c4dd0373ae12ce56b97a6850910ab618957b9586d9
languageName: node
linkType: hard

"@sitecore-cloudsdk/utils@npm:^0.3.0":
version: 0.3.0
resolution: "@sitecore-cloudsdk/utils@npm:0.3.0"
checksum: 4ee3224d4623d7a60b3b48178581b61498fceaa9cc6e0396f7d664537e3a37f41faeaaf3c9503d1e4e95fa9862892f407ef84ea0925458d68af94232598ade54
"@sitecore-cloudsdk/utils@npm:^0.3.1":
version: 0.3.1
resolution: "@sitecore-cloudsdk/utils@npm:0.3.1"
checksum: c6578ec04823a77aaf7cb6f9cdb842c227698507c38b722db0b9fe1c6b21ee169443cc6b9b8ca77f86c9a4dfc835e1c3b85369079c70ef88e555c8243275d9a6
languageName: node
linkType: hard

Expand Down Expand Up @@ -6213,7 +6213,7 @@ __metadata:
version: 0.0.0-use.local
resolution: "@sitecore-jss/sitecore-jss-nextjs@workspace:packages/sitecore-jss-nextjs"
dependencies:
"@sitecore-cloudsdk/personalize": ^0.3.0
"@sitecore-cloudsdk/personalize": ^0.3.1
"@sitecore-jss/sitecore-jss": 22.1.0-canary.58
"@sitecore-jss/sitecore-jss-dev-tools": 22.1.0-canary.58
"@sitecore-jss/sitecore-jss-react": 22.1.0-canary.58
Expand Down Expand Up @@ -6255,8 +6255,8 @@ __metadata:
ts-node: ^10.9.1
typescript: ~4.9.4
peerDependencies:
"@sitecore-cloudsdk/events": ^0.3.0
"@sitecore-cloudsdk/personalize": ^0.3.0
"@sitecore-cloudsdk/events": ^0.3.1
"@sitecore-cloudsdk/personalize": ^0.3.1
next: ^14.1.0
react: ^18.2.0
react-dom: ^18.2.0
Expand Down

0 comments on commit 8482883

Please sign in to comment.