Skip to content

Commit

Permalink
refactor(preact/recommendation): rewording docs, variable name change…
Browse files Browse the repository at this point in the history
…s and slight refactor
  • Loading branch information
korgon committed Sep 9, 2024
1 parent f285488 commit 7db133a
Show file tree
Hide file tree
Showing 17 changed files with 725 additions and 414 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## Recommendations Integration (Legacy)

For integrations using Snap `v0.60.0` and newer, please reference the updated [`integration docs`](https://searchspring.github.io/snap/#/integration-recommendations).
For integrations using Snap `v0.60.0` and newer, please reference the updated [`integration docs`](https://github.com/searchspring/snap/blob/main/docs/INTEGRATION_RECOMMENDATIONS.md).


It is recommended to utilize the [`RecommendationInstantiator`](https://github.com/searchspring/snap/blob/main/packages/snap-preact/src/Instantiators/README.md) for integration of product recommendations. This method allows recommendations to be placed anywhere on the site with a single script block (requires the `bundle.js` script also).
Expand Down
115 changes: 58 additions & 57 deletions docs/INTEGRATION_RECOMMENDATIONS.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
## Recommendations Integration

Changes to the recommendation integration scripts were made in Snap `v.0.60.0`. Legacy Recommmendation Integrations docs can still be found [`here`](https://github.com/searchspring/snap/blob/main/packages/snap-preact/src/Instantiators/README.md)
Changes to the recommendation integration scripts were made in Snap `v0.60.0`. Legacy Recommmendation Integrations docs can still be found [`here`](https://github.com/searchspring/snap/blob/main/docs/INTEGRATION_LEGACY_RECOMMENDATIONS.md)

It is recommended to utilize the [`RecommendationInstantiator`](https://github.com/searchspring/snap/blob/main/packages/snap-preact/src/Instantiators/README.md) for integration of product recommendations. This method allows recommendations to be placed anywhere on the site with a single script block (requires the `bundle.js` script also).
It is recommended to utilize the [`RecommendationInstantiator`](https://github.com/searchspring/snap/blob/main/packages/snap-preact/src/Instantiators/README.md) for integration of product recommendations. This method allows recommendations to be placed anywhere on the page with a single script block (requires the `bundle.js` script also).

```html
<script type="searchspring/recommendations">
globals = {};
globals = {
shopper: {
id: 'snapdev'
}
};
profiles = [
{
profile: 'recently-viewed',
target: '.above-content',
target: '.ss__recs__recently-viewed',
options: {
limit: 5
}
Expand All @@ -19,43 +23,57 @@ It is recommended to utilize the [`RecommendationInstantiator`](https://github.c
</script>
```

The `RecommendationInstantiator` will look for these elements on the page and attempt to inject components based on the `profiles` specified. In the example above, the profile specified is the `recently-viewed` profile, and is set to render inside the target `.above-content`, this profile would typically be setup to display the last products viewed by the shopper. These profiles must be setup in the Searchspring Management Console (SMC) and have associated Snap templates selected.
The `RecommendationInstantiator` will look for these elements on the page and attempt to inject components based on the `profiles` specified. In the example above, the profile specified is the `recently-viewed` profile, and is set to render inside the target `.ss__recs__recently-viewed`, this profile would typically be setup to display the last products viewed by the shopper. These profiles must be setup in the Searchspring Management Console (SMC) and have associated Snap templates selected.


## Recommendation Context Variables
Context variables may be applied to individual recommendation profiles similar to how they are done on the integration script tag. Variables here may be required depending on the profile type utilized, and can be used to alter the results displayed by our recommendations.
Context variables are applied to individual recommendation profiles similar to how they are done on the integration script tag. Variables here may be required depending on the profile placement, and can be used to alter the results displayed by our recommendations.

### Globals Variables
| Option | Value | Page | Description |
|---|---|:---:|---|
| products | array of SKU strings | product detail page | SKU value(s) to identify the current product(s) being viewed |
| cart | array (or function that returns an array) of current cart skus | all | optional method of setting cart contents |
| shopper.id | logged in user unique identifier | all | required for personalization functionallity if not provided to the bundle (global) context |
| Option | Value | Placement | Description | Required
|---|---|:---:|---|:---:|
| products | array of SKU strings | product detail page | SKU value(s) to identify the current product(s) being viewed | ✔️ |
| cart | array (or function that returns an array) of current cart skus | all | optional method of setting cart contents | |
| shopper.id | logged in user unique identifier | all | required for personalization functionallity if not provided to the bundle (global) context | |


### Profile Specific Variables
| Option | Value | Page | Description |
|---|---|:---:|---|
| profile | string | all | profile name to use |
| target | string | all | CSS selector to render component inside |
| options.siteId | global siteId overwrite | all | optional global siteId overwrite |
| options.categories | array of category path strings | all | optional category identifiers used in category trending recommendation profiles |
| options.brands | array of brand strings | all | optional brand identifiers used in brand trending recommendation profiles |
| options.branch | template branch overwrite | all | optional branch overwrite for recommendations template (advanced usage) |
| options.dedupe | boolean (default: `true`) | all | dedupe products across all profiles in the batch |
| options.searchTerm | string | all | query to search |
| options.filters | array of filters | all | optional recommendation filters |
| options.realtime | boolean | all | optional update recommendations if cart contents change (requires [cart attribute tracking](https://github.com/searchspring/snap/blob/main/docs/INTEGRATION_TRACKING.md)) |
| options.blockedItems | array of strings | all | SKU values to identify which products to exclude from the response |
| options.order | number | all | optional order number for recommendation params to be added to the batched request. Profiles that do not specify an order will be placed at the end, in the occurrence they appear in the DOM.
| options.limit | number (default: 20, max: 20) | all | optional maximum number of results to display, can also be set globally [via config globals](https://github.com/searchspring/snap/tree/main/packages/snap-controller/src/Recommendation) |
| Option | Value | Placement | Description | Required
|---|---|:---:|---|:---:|
| profile | string | all | profile name to use | ✔️ |
| target | string | all | CSS selector to render component inside | ✔️ |
| options.siteId | global siteId overwrite | all | optional global siteId overwrite | |
| options.categories | array of category path strings | all | optional category identifiers used in category trending recommendation profiles | |
| options.brands | array of brand strings | all | optional brand identifiers used in brand trending recommendation profiles | |
| options.branch | template branch overwrite | all | optional branch overwrite for recommendations template (advanced usage) | |
| options.dedupe | boolean (default: `true`) | all | dedupe products across all profiles in the batch | |
| options.searchTerm | string | all | query to search | |
| options.filters | array of filters | all | optional recommendation filters | |
| options.realtime | boolean | all | optional update recommendations if cart contents change (requires [cart attribute tracking](https://github.com/searchspring/snap/blob/main/docs/INTEGRATION_TRACKING.md)) | |
| options.blockedItems | array of strings | all | SKU values to identify which products to exclude from the response | |
| options.limit | number (default: 20, max: 20) | all | optional maximum number of results to display, can also be set globally [via config globals](https://github.com/searchspring/snap/tree/main/packages/snap-controller/src/Recommendation) | |


## Batching and Ordering
By default, each script tag will fetch the recommendations for each profile it finds a target for in a single batched request. The priority order is based on the order listed in the profiles array.
In most cases batching everything is the best practice, however for profiles like a mini cart (side cart) de-duplication may not be desired. De-duplication can be turned off per profile with a `dedupe: false` value, or you can add an additional script to fetch the recommendations in a seperate batch.
Each "searchspring/recommendations" script block groups multiple recommendation profiles into a single API request, known as a batch. By default, the script tag fetches recommendations for all profiles with a matching target in one batched request. The order of profiles in the array determines their priority within the batch.

While batching all profiles together is generally the most efficient approach, there may be cases where separate batching is preferred. For instance, recommendations for a mini cart (side cart) might not require de-duplication with other recommendations. You can disable de-duplication for a specific profile by setting `dedupe: false` in its options, or create a separate batch by using an additional script tag.

## Deduping

The example below shows how to manually specify the order and batching of specific profiles.
Deduping is a process that prevents the same product from appearing in multiple recommendation profiles within a single batch. This is particularly useful when you have several recommendation profiles on a page and want to ensure a diverse range of products is shown to the user.

Here's how deduping works:

1. By default, deduping is enabled for all profiles in a batch (`options.dedupe: true`).
2. The order of profiles in the array determines their priority for deduping.
3. When a product is returned for a higher-priority profile, it becomes unavailable for lower-priority profiles in the same batch.

For example, if you have three profiles in this order: "Customers Also Bought", "Similar Products", and "You May Also Like", and a product is returned for "Customers Also Bought", it won't appear in "Similar Products" or "You May Also Like".

You can disable deduping for specific profiles by setting `options.dedupe: false`. This is useful for profiles where you want to ensure certain products always appear, regardless of their presence in other recommendations.

Here's an example that demonstrates deduping:

```html
<script type="searchspring/recommendations">
Expand All @@ -65,49 +83,32 @@ The example below shows how to manually specify the order and batching of specif
profiles = [
{
profile: 'customers-also-bought',
target: '.crosssell',
target: '.ss__recs__crosssell',
options: {
limit: 5
}
},
{
profile: 'customers-also-viewed',
target: '.similar'
target: '.ss__recs__similar'
},
//same batch, but dedupe false
// same batch, but dedupe false
{
profile: 'bundle',
target: '.similar',
profile: 'customers-also-like',
target: '.ss__recs__alsoliked',
options: {
dedupe: false
}
}
];
</script>

<!-- seperate batch -->
<script type="searchspring/recommendations">
globals = {
products: ['product123']
};
profiles = [
{
profile: 'quick-cart',
target: '.crosssell',
options: {
limit: 5
}
}
];
</script>
```

## Additional Examples

The examples below assume that the `similar` profile has been setup in the Searchspring Management Console (SMC), and that a Snap `bundle.js` script exists on the page and has been configured with a `RecommendationInstantiator`.

A typical "similar" profile that would display products similar to the product passed in via the `product` context variable.
A typical "similar" profile that would display products similar to the product passed in via the `products` global context variable.

```html
<script type="searchspring/recommendations">
Expand All @@ -117,7 +118,7 @@ A typical "similar" profile that would display products similar to the product p
profiles = [
{
profile: 'customers-also-viewed',
target: '.similar'
target: '.ss__recs__similar'
}
];
</script>
Expand All @@ -133,7 +134,7 @@ If tracking scripts are not in place, "crosssell" profiles may require the cart
profiles = [
{
profile: 'customers-also-bought',
target: '.crosssell'
target: '.ss__recs__crosssell'
}
];
</script>
Expand All @@ -151,21 +152,21 @@ If the shopper identifier is not beeing captured by the `bundle.js` context, it
profiles = [
{
profile: 'view-cart',
target: '.cart'
target: '.ss__recs__cart'
}
];
</script>
```

### Filters
The example shown below will filter the recommendations for products matching color: blue, & red, and price range 0 - 20.
The example shown below will filter the recommendations for products matching field `color` with a value `blue` and `red`, as well as a field `price` with a range from `0` to `20`.

```html
<script type="searchspring/recommendations">
profiles = [
{
profile: 'customers-also-bought',
target: '.crosssell',
target: '.ss__recs__crosssell',
options: {
filters: [
{
Expand Down
8 changes: 8 additions & 0 deletions docs/documents.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,14 @@ var documents = [
url: './docs/INTEGRATION_RECOMMENDATIONS.md',
searchable: true,
},
{
label: 'Recommendations (legacy)',
route: '/integration-legacy-recommendations',
type: 'markdown',
url: './docs/INTEGRATION_LEGACY_RECOMMENDATIONS.md',
searchable: true,
hidden: true,
},
{
label: 'Tracking',
route: '/integration-tracking',
Expand Down
3 changes: 2 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
{ a: "(https://github.com/searchspring/snap/blob/main/docs/INTEGRATION_DEBUGGING.md)", b: "(#/integration-debugging)"},
{ a: "(https://github.com/searchspring/snap/blob/main/docs/INTEGRATION_TRACKING.md)", b: "(#/integration-tracking)"},
{ a: "(https://github.com/searchspring/snap/blob/main/docs/INTEGRATION_RECOMMENDATIONS.md)", b: "(#/integration-recommendations)"},
{ a: "(https://github.com/searchspring/snap/blob/main/docs/INTEGRATION_LEGACY_RECOMMENDATIONS.md)", b: "(#/integration-legacy-recommendations)"},

{ a: "https://github.com/searchspring/snap/blob/main/images/snap-dependencies.jpg?raw=true", b: "./images/snap-dependencies.jpg"},
{ a: "https://github.com/searchspring/snap/blob/main/images/snap-dependencies.png?raw=true", b: "./images/snap-dependencies.png"},
Expand Down Expand Up @@ -244,7 +245,7 @@
app.component('Link', {
props: ['link', 'active'],
template: `
<router-link v-if="link.route" :key="link.route" :to="'' + link.route" :class="{ 'active': active }">
<router-link v-if="link.route && !link.hidden" :key="link.route" :to="'' + link.route" :class="{ 'active': active }">
{{link.label}}
</router-link>
Expand Down
6 changes: 3 additions & 3 deletions packages/snap-client/src/Client/apis/Legacy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,16 @@ describe('Legacy Api', () => {
.mockImplementation(() => Promise.resolve({ status: 200, json: () => Promise.resolve({}) } as Response));

const params = {
body: '{"siteId":"88uyt2m"}',
body: '{"siteId":"abc123"}',
headers: {
'Content-Type': 'application/json',
},
method: 'POST',
};
const reuestUrl = 'https://88uyt2m.a.searchspring.io/api/meta/meta.json';
const reuestUrl = 'https://abc123.a.searchspring.io/api/meta/meta.json';

await api.postMeta({
siteId: '88uyt2m',
siteId: 'abc123',
});

expect(requestMock).toHaveBeenCalledWith(reuestUrl, params);
Expand Down
13 changes: 4 additions & 9 deletions packages/snap-client/src/Client/apis/Recommend.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,22 +57,22 @@ describe('Recommend Api', () => {

const params = {
method: 'POST',
body: '{"siteId":"88uyt2m","profiles":[{"tag":"dress"}]}',
body: '{"siteId":"8uyt2m","profiles":[{"tag":"dress"}]}',
headers: {
'Content-Type': 'text/plain',
},
};

const requestParameters: PostRecommendAPISpec = {
siteId: '88uyt2m',
siteId: '8uyt2m',
profiles: [
{
tag: 'dress',
},
],
};

const requestUrl = 'https://88uyt2m.a.searchspring.io/boost/88uyt2m/recommend';
const requestUrl = 'https://8uyt2m.a.searchspring.io/boost/8uyt2m/recommend';

const requestMock = jest
.spyOn(global.window, 'fetch')
Expand Down Expand Up @@ -151,7 +151,7 @@ describe('Recommend Api', () => {
batched: true,
...batchParams,
});
// @ts-ignore

api.batchRecommendations({
tag: 'crossSell',
limits: 10,
Expand Down Expand Up @@ -323,7 +323,6 @@ describe('Recommend Api', () => {
.spyOn(global.window, 'fetch')
.mockImplementation(() => Promise.resolve({ status: 200, json: () => Promise.resolve(response) } as Response));

// @ts-ignore
const promise1 = api.batchRecommendations({
tag: 'similar',
categories: ['shirts'],
Expand All @@ -333,7 +332,6 @@ describe('Recommend Api', () => {
...batchParams,
});

// @ts-ignore
const promise2 = api.batchRecommendations({
tag: 'crosssell',
categories: ['dress'],
Expand Down Expand Up @@ -433,7 +431,6 @@ describe('Recommend Api', () => {
.spyOn(global.window, 'fetch')
.mockImplementation(() => Promise.resolve({ status: 200, json: () => Promise.resolve(mockData.recommend()) } as Response));

// @ts-ignore
api.batchRecommendations({ tag: 'crossSell', products: ['some_sku', 'some_sku2'], ...batchParams });

//add delay for paramBatch.timeout
Expand All @@ -458,7 +455,6 @@ describe('Recommend Api', () => {
.spyOn(global.window, 'fetch')
.mockImplementation(() => Promise.resolve({ status: 200, json: () => Promise.resolve(mockData.recommend()) } as Response));

// @ts-ignore
api.batchRecommendations({ tag: 'crossSell', blockedItems: ['blocked_sku1', 'blocked_sku2'], ...batchParams });

//add delay for paramBatch.timeout
Expand Down Expand Up @@ -527,7 +523,6 @@ describe('Recommend Api', () => {
.mockImplementation(() => Promise.resolve({ status: 200, json: () => Promise.resolve({}) } as Response));

for (let i = 0; i < 100; i++) {
// @ts-ignore
api.batchRecommendations({
tag: i.toString(),
...batchParams,
Expand Down
15 changes: 7 additions & 8 deletions packages/snap-client/src/Client/apis/Recommend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ class Deferred {
}
}

type CombinedIncoming = Partial<RecommendRequestModel>;
type BatchEntry = {
request: CombinedIncoming;
request: RecommendRequestModel;
deferred: Deferred;
};

Expand Down Expand Up @@ -55,22 +54,22 @@ export class RecommendAPI extends API {
}

async batchRecommendations(parameters: RecommendRequestModel): Promise<RecommendResponseModel> {
const groupId = parameters.groupId || 1;
const batchId = parameters.batchId || 1;

// set up batch key and deferred promises
const key = parameters.batched ? `${parameters.siteId}:${groupId}` : `${Math.random()}:${groupId}`;
const key = parameters.batched ? `${parameters.siteId}:${batchId}` : `${Math.random()}:${batchId}`;
const batch = (this.batches[key] = this.batches[key] || { timeout: null, request: { profiles: [] }, entries: [] });
const deferred = new Deferred();

const { tag, limits, limit, searchTerm, filters, profileFilters, dedupe, categories, brands } = parameters;
const { tag, limits, limit, query, filters, profileFilters, dedupe, categories, brands } = parameters;

const newParams = {
...parameters,
tag: tag,
categories,
brands,
limit: limit ? limit : limits ? (typeof limits == 'number' ? limits : limits[0]) : undefined,
searchTerm,
searchTerm: query,
profileFilters,
dedupe,
filters,
Expand All @@ -93,7 +92,7 @@ export class RecommendAPI extends API {
// now that the requests are in proper order, map through them
// and build out the batches
batch.entries.map((entry) => {
const { tag, categories, brands, searchTerm, profileFilters, dedupe } = entry.request;
const { tag, categories, brands, query, profileFilters, dedupe } = entry.request;
let limit = entry.request.limit;

if (!limit) {
Expand All @@ -105,7 +104,7 @@ export class RecommendAPI extends API {
categories,
brands,
limit,
searchTerm,
query,
filters: profileFilters,
dedupe,
};
Expand Down
Loading

0 comments on commit 7db133a

Please sign in to comment.