Dev GuideAPI Reference
Dev GuideAPI ReferenceUser GuideGitHubNuGetDev CommunityOptimizely AcademySubmit a ticketLog In
Dev Guide

Client-side API integration

Describes how to integrate Optimizely Product Recommendations client-side API to Optimizely Commerce Connect.

📘

Note

Sample code in this article is taken from Quicksilver templates. The Quicksilver Commerce Connect sample site and installation instructions are available to download from GitHub.

  1. See Commerce Connect API overview for the initialization steps.
  2. Define the recommended sections. In the Views/Shared/_Layout.cshtml view, add the following sections:
    ...
    <div class="main-area">@RenderBody()</div>
      <div class="recommendations recommendations-upper"></div>
      <div class="recommendations recommendations-left"></div>
      <div class="recommendations recommendations-right"></div>
      <div class="recommendations recommendations-bottom"></div>
    </div>
    ...
  3. Add the client-side tracking script. In the Views/Shared/_Layout.cshtml view, add the following @using statement to the top of the file.
    @using EPiServer.Personalization.Commerce.Extensions
  4. Then add @Html.LoadTrackingAPI() to the <head> tag:
    ...
    <head>
      ...
      @Html.LoadTrackingAPI()
    </head>
    ...
  5. Set up the template engine to render recommended data. This example uses the mustache.js template engine. The following code resides in a partial view named Views/Shared/_RecommendationsTemplates.cshtml:
    <script id="epiRecommendationListTemplate" type="x-tmpl-mustache">
        {{#recs}}
            <div class="jsProductTile product-row__tile" data-recommendation-id="{{id}}">
                {{> epiRecommendationItemTemplate}}
            </div>
        {{/recs}}
    </script>
    
    <script id="epiRecommendationItemTemplate" type="x-tmpl-mustache">
        {{#hasDiscount}}
            <div class="product has-discount">
        {{/hasDiscount}}
        {{^hasDiscount}}
            <div class="product">
        {{/hasDiscount}}
            <a href="{{#attributes}}{{url}}{{/attributes}}&recommendationId={{id}}" class="link--black">
                <div class="view-details"></div>
                <img src="{{#attributes}}{{img}}{{/attributes}}" alt="{{refCode}}" class="img-responsive" />
                <h3 class="product-title">{{#attributes}}{{title}}{{/attributes}}</h3>
                <div>
                    {{#hasDiscount}}
                        <h4 class="product-price">{{#attributes}}{{unitPrice}}{{/attributes}}</h4>
                        <h4 class="product-price product-price--discount">{{#attributes}}{{salePrice}}{{/attributes}}</h4>
                    {{/hasDiscount}}
                    {{^hasDiscount}}
                        <h4 class="product-price">{{#attributes}}{{salePrice}}{{/attributes}}</h4>
                    {{/hasDiscount}}
                </div>
            </a>
        </div>
        <div class="quick-view-btn-container">
            <button type="button"
                    data-toggle="modal"
                    data-target="#ModalDialog"
                    data-url="{{#attributes}}{{url}}{{/attributes}}&recommendationId={{id}}"
                    class="btn btn-block btn-sm quickview-button">@Html.Translate("/Product/Quickview")
            </button>
        </div>
    </script>
  6. Render recommendation templates. In the Views/Shared/_Layout.cshtml view, render the templates you created:
    <body>
      ...
      @Html.Partial("_RecommendationsTemplates")
      ...
    </body>
  7. Render the page tracking script. In the Views/Shared/_Layout.cshtml view, add the following code to render the page tracking script:
    @RenderSection("Tracking", false)
    The page now looks like the following:
    <body>
      ...
      @Html.Partial("_RecommendationsTemplates")
      @RenderSection("Tracking", false)
    </body>

Track with custom attributes

This section explains how to create, send, and consume recommendations on a page with custom attributes. The integration is applied to a product package page.

  1. Create product tracking data:
    var refCode = '@Model.Package.Code';
    var packageTrackingData = TrackingDataFactory.createProductTrackingData(refCode);
  2. Add a custom attribute:
    packageTrackingData["customAttributes"] = { 'marketId': Market.getSelectedMarketId() };
  3. Send a tracking request and receive recommendations:
    epiRecommendations.track(
        packageTrackingData,
        null,
        Recommendations.render, {
            sectionMappings: [
                {
                    area: "productAlternativesWidget",
                    selector: ".recommendations-right",
                    numberOfItemsToRender: 2
                },
                {
                    area: "productCrossSellsWidget",
                    selector: ".recommendations-bottom"
                }
            ]
        }
    );
  4. In the Views/Packages/Index.cshtml view, use these settings to render more than one recommendation section:
    @section Tracking {
        <script>
            $(document).ready(function () {
                var refCode = '@Model.Package.Code';
                var packageTrackingData = TrackingDataFactory.createProductTrackingData(refCode);
                packageTrackingData["customAttributes"] = {
                    'marketId': Market.getSelectedMarketId()
                };
                epiRecommendations.track(
                    packageTrackingData,
                    null,
                    Recommendations.render, {
                        sectionMappings: [
                            {
                                area: "productAlternativesWidget",
                                selector: ".recommendations-right",
                                numberOfItemsToRender: 2
                            },
                            {
                                area: "productCrossSellsWidget",
                                selector: ".recommendations-bottom"
                            }
                        ]
                    }
                );
            });
        </script>
    }

The section mapping defines options to render the recommended data:

  • First section mapping:
    • Type: productAlternativesWidget
    • Maximum items: 2
    • Target HTML element with CSS class recommendations-right
  • Second section mapping:
    • Type: productCrossSellsWidget
    • Maximum items: none
    • Target HTML element with CSS class recommendations-bottom