Techdocs
  • Introduction
  • Implementing Nosto
    • Implement on your website
      • Manual Tagging - Essentials
        • Setting up your account
        • Adding the Nosto Script
        • Adding the Cart Tagging
        • Adding the Customer information
        • Adding the Product Tagging
          • Default Product Tagging
          • Basic Tagging
        • Adding the Category/Brand Tagging
        • Adding the Search Tagging
        • Adding the Order Tagging
        • Defining Nosto placements
        • Tagging your page types
      • Advanced Usage
        • Extending tagging with SKUs
        • Adding support for multi-currency
        • Adding support for customer group pricing
      • FAQ
    • Implement on native mobile
    • Implement on a physical store
    • Implement Search & Categories
      • Using Search Templates
        • Using Search Templates
        • Implementing Search page
        • Implementing Autocomplete
        • Implementing Category pages
        • Testing & deployment
        • FAQ
      • Using the API
        • Use the Search & Categories API
        • Implement Search results page
        • Implement Autocomplete
        • Implement Category pages
        • FAQ
      • Using the JavaScript Library
        • Implement Autocomplete using the Nosto Autocomplete library
          • Installation
          • Initialization
            • Render results
            • Submit search
          • Create Autocomplete template
          • Further reading
    • Check your setup
    • Template customization
      • Starting points
      • Product cards
      • Styling
      • Scripting
  • APIs
    • GraphQL
      • The Playground
      • Using the API
      • Testing and Debugging
      • Using Mutations
        • Updating Products
        • Updating Categories
        • Updating Identities
        • GraphQL: Onsite Sessions
        • Working with Orders
          • GraphQL: Placing Orders
          • GraphQL: Updating Order Statuses
      • Using Queries
        • Querying Products
        • Querying Identities
        • Querying Orders
        • Querying Recommendations
        • Querying Segments
        • Querying Search
        • Querying Category Merchandising Products (CM 1.0)
      • For iOS & Android
      • For Headless
    • REST
      • GDPR
        • Redacting customer data
        • Initiating data takeouts
      • Customers
        • Blacklisting Customers
        • Toggling marketing consent
      • Products
        • Updating Products
        • Discontinuing Products
        • Recrawling Products
      • Other
        • Updating Rates
    • Frontend
      • Session API
        • Terminology
        • Setting up
        • Managing Sessions
        • Handling Placements
        • Tracking Events
        • Leveraging Features
        • Record Attribution
        • Advanced Usage
          • Supporting opt-out and do-not-track
          • Using external session identifiers
          • Adding support for multi-currency
          • Adding support for customer group pricing
        • FAQ
      • JS API
        • Initializing Nosto
        • Recommendations
          • Loading Recommendations
          • Recommendation Callbacks
          • Setting up dynamic filtering
          • Sending Product-View Events
          • Sending Add to Cart-Events
        • Popups
          • Listing Popup Campaigns
          • Opening a Popup
          • Enabling & Disabling Popups
          • Popup Callbacks
        • Advanced Usage
          • Sending email addresses to Nosto
          • Manually segmenting users
          • Dynamically sending the cart content
          • Sending Customer Information
        • Record Attribution
      • Open Source
        • Nosto JS
        • Search JS
        • Nosto React
        • Web Components
          • Loading Web components
  • User Generated Content
    • UGC Techdocs
  • 3rd party data integrations
    • Shopify
    • Magento 2
    • Magento
    • BigCommerce
    • Shopware 6
    • Shopware 5
    • Prestashop
    • Salesforce
    • PHP-SDK
Powered by GitBook
On this page
  • Working with recommendations
  • Reporting correct page views - load() vs update()
  • Reporting correct product views - requesting product recommendations outside product detail page
  • Handling attribution
  • Letting Nosto render the HTML
  • Including Add to Cart-buttons into Recommendations
  • Working with content
  • Working with popups
  • Working with affinities

Was this helpful?

Export as PDF
  1. APIs
  2. Frontend
  3. Session API

Leveraging Features

Working with recommendations

The load method returns a promise, which can be consumed to get the raw recommendation data. The raw recommendation data is an object containing the recommendation data.

Reporting correct page views - load() vs update()

Recommendation results are returned in a promise by calling either load() or update(). The difference between these two methods is:

  • load() causes the load count of the most recently tracked page view to be incremented.

  • update() does not automatically increment the page load count.

In order to achieve an accurate reach statistic for recommendations, load() should be called once per page view when fetching recommendations and all proceeding recommendation requests should use update().

Reporting correct product views - requesting product recommendations outside product detail page

Normally requesting product related recommendations happens in relation to viewing a product detail page, so a typical product related recommendation request looks like this:

nostojs(api => {
  api.defaultSession()
    .setResponseMode('HTML')
    .enableCampaignInjection()
    .viewProduct('product1') // id of product currently being viewed
    .setPlacements(api.placements.getPlacements())
    .load()
});

The above will also add a product view event for the user. In some cases there are needs to request product related recommendations even when the user is not looking at a product. In these cases, to avoid reporting the user event, include a { trackEvents: false } flag to the load function, for example:

nostojs(api => {
  api.defaultSession()
    .setResponseMode('HTML')
    .enableCampaignInjection()
    .viewProduct('product1') // id of product currently being viewed
    .setPlacements(api.placements.getPlacements())
    .load({ trackEvents: false })
});

If for implementation specific reasons there are multiple calls for product related recommendations on the product page, only the first request should track the event and subsequent requests should have the { trackEvents: false } flag include.

Handling attribution

When a recommended product is viewed, the result_id from the recommendation result should be used. Below is an example recommendation result (some fields have been omitted).

{
    "recommendations": {
        "nosto-frontpage-1": { // requested placement id
            "result_id": "nosto-frontpage-1-fallback", // slot id that served the recommendations
            "products": [{
                "url": "https://example.com/products/product1",
                "product_id": "product1"
            }, {
                "url": "https://example.com/products/product2",
                "product_id": "product2"
            }],
            "result_type": "REAL",
            "title": "Most Popular Right Now",
            "div_id": "nosto-frontpage-1" // requested placement id
    }
}

The snippet of code below attributes product1 to the result id nosto-frontpage-1-fallback from the recommendation result above and fetches recommendations for nosto-productpage-1.

nostojs(api => {
  api.defaultSession()
   .viewProduct('product1') // id of product currently being viewed
   .setRef('product1', 'nosto-frontpage-1-fallback') // id of product and slot that resulted in the product view
   .setPlacements(['nosto-productpage-1']) // placements to request content for on the product page
   .load()
   .then(data => console.log(data))
 })

Letting Nosto render the HTML

If you wish Nosto to render the recommendations and returns HTML for the recommendations you can set the response mode to HTML by adding .setResponseMode('HTML') into your call. A full example for 404 page would look like this

nostojs(api => {
  api.defaultSession()
    .setResponseMode('HTML')
    .viewNotFound()
    .setPlacements(['notfound-nosto-1', 'bestseller-recs'])
    .load()
    .then(data => {
      // Here you would inject the recommendations to DOM
      console.log(data.recommendations);
    })
});

Please note that Nosto will not inject the returned HTML into the DOM even if the response mode is set to HTML. The logic for injecting the recommendations must be implemented in the application side.

Including Add to Cart-buttons into Recommendations

Usually adding an Add to cart button directly into the recommendation which lists multiple products is fairly straightforward, the website just needs to have an appropriate javascript function to do the operation. Nosto provides ready-made functions for multiple platforms so check the Nosto documentation specific to your e-commerce platform or if needed look into your e-commerce platform's documentation and support on how to create such a function.

Once the Add to cart button is pressed it should call that function to add the products to the cart, but it should also additionally let Nosto know that the button was pressed so Nosto can track the sales from the recommendation. This can be done with the reportAddToCart function as follows:

nostojs(api => {
  api.defaultSession()
    .reportAddToCart('product-id', 'element')
    .update()
});

⚠️ The api call is only for informing Nosto about the attribution (that the product was added from the recommendation), setCart function in the Session API should be used to tell Nosto the user’s cart contents.

Working with content

The load method returns a promise which can be consumed to get the raw placement data. The raw recommendation data is an object containing the placement data.

Working with popups

Working with affinities

Customer affinities are taken into account by the recommendation engine and matched with the affinities defined on the products. The affinities are determined automatically, but the API allows for a more granular manual control when required.

In case addAffinity is called multiple times, the provided values will all be merged together.

Note: If the same key is provided more than once, the latest encountered value is used, and the earlier keys are overridden.

nostojs(api => {
  api.defaultSession()
      .viewFrontPage()
      .addAffinity({ brand: ["nike"] })
      .addAffinity({ productType: ["shoes"] })
      .load()
      .then(handleResponse)
})

It's possible to provide multiple affinities in a single call to addAffinity.

nostojs(api => {
  api.defaultSession()
      .viewFrontPage()
      .addAffinity({ productType: ["shoes"], "sku.size": ["42"] })
      .load()
      .then(handleResponse)
})

If the current state of the request needs to be cleared, a second parameter can be provided to the addAffinity call. In the example below, sku.size key will be removed from the request.

Note: This only affects the current local state of the request. It will not remove user's affinities already saved on the backend.

nostojs(api => {
  api.defaultSession()
      .viewFrontPage()
      .addAffinity({ "sku.size": ["42"] })
      .addAffinity({ productType: ["shoes"] }, { clear: true })
      .load()
      .then(handleResponse)
})
PreviousTracking EventsNextRecord Attribution

Last updated 9 days ago

Was this helpful?

Popups are handled automatically and you do not need to control the rendering of the popups. As events are dispatched, popups will show and hide automatically. If you would like to have programmatic control over the popups, see .

our guide on the Popup JS API