Skip to content

Commit

Permalink
[Personalize] Updates to CDP executing experience call based on Boxev…
Browse files Browse the repository at this point in the history
…er team input (#1077)

* [Personalize] Updates to CDP executing experience call based on Boxever team input

* Make `isTimeoutError` shared
  • Loading branch information
illiakovalenko authored Jun 28, 2022
1 parent 0d99268 commit 886267f
Show file tree
Hide file tree
Showing 9 changed files with 304 additions and 160 deletions.
31 changes: 18 additions & 13 deletions packages/sitecore-jss-nextjs/src/edge/personalize-middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
GraphQLPersonalizeServiceConfig,
CdpService,
CdpServiceConfig,
ExperienceContext,
ExperienceParams,
getPersonalizedRewrite,
} from '@sitecore-jss/sitecore-jss/personalize';
import { debug, NativeDataFetcher } from '@sitecore-jss/sitecore-jss';
Expand Down Expand Up @@ -83,7 +83,7 @@ export class PersonalizeMiddleware {
}
}

protected getExperienceContext(req: NextRequest): ExperienceContext {
protected getExperienceParams(req: NextRequest): ExperienceParams {
return {
geo: {
city: req.geo?.city ?? null,
Expand Down Expand Up @@ -157,28 +157,35 @@ export class PersonalizeMiddleware {
return response;
}

if (!browserId) {
browserId = await this.cdpService.generateBrowserId();

if (!browserId) {
debug.personalize('skipped (browser id generation failed)');
return response;
}
}

// Execute targeted experience in CDP
const context = this.getExperienceContext(req);
const experienceResult = await this.cdpService.executeExperience(
const params = this.getExperienceParams(req);
const variantId = await this.cdpService.executeExperience(
personalizeInfo.contentId,
context,
params,
browserId
);
// If a browserId was not passed in (new session), a new browserId will be returned
browserId = experienceResult.browserId;

if (!experienceResult.variantId) {
if (!variantId) {
debug.personalize('skipped (no variant identified)');
return response;
}

if (!personalizeInfo.variantIds.includes(experienceResult.variantId)) {
if (!personalizeInfo.variantIds.includes(variantId)) {
debug.personalize('skipped (invalid variant)');
return response;
}

// Rewrite to persononalized path
const rewritePath = getPersonalizedRewrite(pathname, { variantId: experienceResult.variantId });
const rewritePath = getPersonalizedRewrite(pathname, { variantId });
// Note an absolute URL is required: https://nextjs.org/docs/messages/middleware-relative-urls
const rewriteUrl = req.nextUrl.clone();
rewriteUrl.pathname = rewritePath;
Expand All @@ -189,9 +196,7 @@ export class PersonalizeMiddleware {
response.headers.set('x-middleware-cache', 'no-cache');

// Set browserId cookie on the response
if (browserId) {
this.setBrowserId(response, browserId);
}
this.setBrowserId(response, browserId);

debug.personalize('personalize middleware end: %o', {
rewritePath,
Expand Down
2 changes: 1 addition & 1 deletion packages/sitecore-jss/src/graphql-request-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export class GraphQLRequestClient implements GraphQLClient {
resolve(data);
})
.catch((error: ClientError) => {
this.debug('response error: %o', error.response);
this.debug('response error: %o', error.response || error.message || error);
reject(error);
});
});
Expand Down
Loading

0 comments on commit 886267f

Please sign in to comment.