The Session API provides a programmatic way to interact with the Nosto API that is optimized for usage in SPA style web applications. The Session object maintains the session level data such as the user's shopping cart, the currently logged in customer, the current variation and provides function to execute actions such as viewing a product, category, cart, order etc.

interface Session {
    addOrder(order: WebsiteOrder): Action;
    recordAttribution(
        type: EventType,
        target: string,
        ref?: string,
        refSrc?: string,
    ): Attribution;
    reportAddToCart(product: string, element: string): Action;
    setCart(cart: undefined | Cart): Session;
    setCustomer(customer: undefined | PushedCustomer): Session;
    setResponseMode(mode: RenderMode): Session;
    setRestoreLink(restoreLink: string): Session;
    setVariation(variation: undefined | string): Session;
    viewCart(): Action;
    viewCategory(...categories: string[]): Action;
    viewCustomField(customFields: Record<string, string[]>): Action;
    viewFrontPage(): Action;
    viewNotFound(): Action;
    viewOther(): Action;
    viewProduct(product: string | Product): Action;
    viewProductSku(productId: string, skuId: string): Action;
    viewSearch(...searchTerms: string[]): Action;
    viewTag(...tags: string[]): Action;
}

Methods

  • Create a new action for an order page. This should only be used on order confirmation / thank you pages.

    You do not need to specify the page-type explicitly as it is inferred from the action.

    You must invoke [the load method]Action#load on the resultant action in order for the request to be made.

    Parameters

    • order: WebsiteOrder

      the information about the order that was placed

    Returns Action

    the action instance to load content or track events.

    nostojs(api => {
    api.defaultSession()
    .addOrder({
    external_order_ref: "145000006",
    info: {
    order_number: "195",
    email: "mridang@nosto.com",
    first_name: "Mridang",
    last_name: "Agarwalla",
    type: "order",
    newsletter: true
    },
    items: [{
    product_id: "406",
    sku_id: "243",
    name: "Linen Blazer (White, S)",
    quantity: 1,
    unit_price: 455,
    price_currency_code: "EUR"
    }]
    })
    .setPlacements(["order-related"])
    .load()
    .then(data => {
    console.log(data.recommendations);
    })
    })
  • Parameters

    • type: EventType
    • target: string
    • Optionalref: string
    • OptionalrefSrc: string

    Returns Attribution

    nostojs(api => api
    .defaultSession()
    .recordAttribution("vp", "12345678", "123456")
    .done()
    .then(data => console.log(data))
  • Creates an action to report that product was added to the shopping cart, e.g. from the recommendation slot with "Add to cart" button.

    You must invoke [the load method]Action#load on the resultant action in order for the request to be made.

    Parameters

    • product: string
    • element: string

    Returns Action

    the action instance to load content or track events.

    nostojs(api => api
    .defaultSession()
    .reportAddToCart("123", "reco-slot-1")
    .load()
    .then(data => console.log(data)))
  • Sets the information about the user's current shopping cart. It the user does not have any items in his shopping cart, you can pass null. Passing null will nullify the user's shopping cart on Nosto's end. You must also pass in the shopping cart content in it's entirety as partial content are not supported.

    Parameters

    • cart: undefined | Cart

      the details of the user's shopping cart contents

    Returns Session

    the current session

    nostojs(api => api
    .defaultSession()
    .setCart({
    items: [
    product_id: "101",
    sku_id: "101-S",
    name: "Shoe",
    unit_price: 34.99
    price_currency_code: "EUR"
    ]
    })
    .viewCart()
    .setPlacements(["free-shipper"])
    .update()
    .then(data => console.log(data)))
  • Sets the information about the currently logged in customer. If the current customer is not provided, you will not be able to leverage features such as triggered emails. While it is recommended to always provide the details of the currently logged in customer, it may be omitted if there are concerns about privacy or compliance.

    Parameters

    • customer: undefined | PushedCustomer

      the details of the currently logged in customer

    Returns Session

    the current session

    nostojs(api => api
    .defaultSession()
    .setCustomer({
    first_name: "Mridang",
    last_name: "Agarwalla",
    email: "mridang@nosto.com",
    newsletter: false,
    customer_reference: "5e3d4a9c-cf58-11ea-87d0-0242ac130003"
    })
    .viewCart()
    .setPlacements(["free-shipper"])
    .load()
    .then(data => console.log(data)))
  • Sets the response type to HTML or JSON_ORIGINAL. This denotes the preferred response type of the recommendation result. If you would like to access the raw recommendation data in JSON form, specify JSON. When you specify JSON, you will need to template the result yourself. If you require a more simplified approach, specify HTML. When you specify HTML, you get back HTML blobs, that you may simply inject into you placements.

    Parameters

    • mode: RenderMode

      the response mode for the recommendation data

    Returns Session

    the current session

    nostojs(api => api
    .defaultSession()
    .setResponseMode("HTML")
    .viewCart()
    .setPlacements(["free-shipper"])
    .load()
    .then(data => console.log(data)))
  • Sets the restore link for the current session. Restore links can be leveraged in email campaigns. Restore links allow the the user to restore the cart contents in a single click.

    Read more about to leverage the restore cart link

    Parameters

    • restoreLink: string

      the secure URL to restore the user's current session

    Returns Session

    the current session

    nostojs(api => api
    .defaultSession()
    .setRestoreLink("https://jeans.com/session/restore?sid=6bdb69d5-ed15-4d92")
    .viewCart()
    .setPlacements(["free-shipper"])
    .load()
    .then(data => console.log(data)))
  • Sets the current variation identifier for the session. A variation identifier identifies the current currency (or the current customer group). If your site uses multi-currency, you must provide the ISO code current currency being viewed.

    Parameters

    • variation: undefined | string

      the case-sensitive identifier of the current variation

    Returns Session

    the current session

    nostojs(api => api
    .defaultSession()
    .setVariation("GBP")
    .viewCart()
    .setPlacements(["free-shipper"])
    .load()
    .then(data => console.log(data)))
  • Create a new action for a cart page. This should be used on all cart and checkout pages. If your site has a multi-step checkout, it is recommended that you send this event on each checkout page.

    You must invoke [the load method]Action#load on the resultant action in order for the request to be made.

    You do not need to specify the page-type explicitly as it is inferred from the action.

    Returns Action

    the action instance to load content or track events.

    nostojs(api => api
    .defaultSession()
    .viewCart()
    .setPlacements(["free-shipper"])
    .load()
    .then(data => console.log(data)))
  • Create a new action for a category page. This should be used on all category, collection of brand pages.

    You must invoke [the load method]Action#load on the resultant action in order for the request to be made.

    You do not need to specify the page-type explicitly as it is inferred from the action.

    Parameters

    • ...categories: string[]

    Returns Action

    the action instance to load content or track events.

    nostojs(api => api
    .defaultSession()
    .viewCategory("/men/shoes")
    .setPlacements(["category123"])
    .load()
    .then(data => console.log(data)))
  • Create a new action with custom fields.

    You must invoke [the load method]Action#load on the resultant action in order for the request to be made.

    You do not need to specify the page-type explicitly as it is inferred from the action. Note: tags are not case-sensitive.

    Parameters

    • customFields: Record<string, string[]>

      custom fields being viewed.

    Returns Action

    the action instance to load content or track events.

    as this is an advanced action with a limited a use case

    nostojs(api => api
    .defaultSession()
    .viewCustomField({material: "cotton"})
    .load()
    .then(data => console.log(data)))
  • Create a new action for a front page. This should be used when the user visits the home page.

    You must invoke [the load method]Action#load on the resultant action in order for the request to be made.

    You do not need to specify the page-type explicitly as it is inferred from the action.

    Returns Action

    the action instance to load content or track events.

    nostojs(api => api
    .defaultSession()
    .viewFrontPage()
    .setPlacements(["best-seller"])
    .load()
    .then(data => console.log(data)))
  • Create a new action for a not found page. This should be used only on 404 pages.

    You must invoke [the load method]Action#load on the resultant action in order for the request to be made.

    You do not need to specify the page-type explicitly as it is inferred from the action.

    Returns Action

    the action instance to load content or track events.

    nostojs(api => api
    .defaultSession()
    .viewNotFound()
    .setPlacements(["best-seller"])
    .load()
    .then(data => console.log(data)))
  • Create a new action for a general page. This should be used only on pages that don't have a corresponding action. For example, if the user is viewing a page such as a "Contact Us" page, you should use the viewOther action.

    You must invoke [the load method]Action#load on the resultant action in order for the request to be made.

    You do not need to specify the page-type explicitly as it is inferred from the action.

    Returns Action

    the action instance to load content or track events.

    nostojs(api => api
    .defaultSession()
    .viewOther()
    .load()
    .then(data => console.log(data)))
  • Create a new action for a product page. This must be used only when a product is being viewed. In case a specific SKU of the product is being viewed, use viewProductSku instead.

    You must invoke [the load method]Action#load on the resultant action in order for the request to be made.

    You do not need to specify the page-type explicitly as it is inferred from the action.

    Parameters

    Returns Action

    the action instance to load content or track events.

    nostojs(api => api
    .defaultSession()
    .viewProduct("101")
    .setCategories(["/men/trousers"])
    .setRef("123", "example_reco_id")
    .setPlacements(["cross-seller"])
    .load()
    .then(data => console.log(data)))
  • Create a new action for a product page when a specific SKU has been chosen. This must be used only when a product and specific SKU is being viewed.

    You must invoke [the load method]Action#load on the resultant action in order for the request to be made.

    You do not need to specify the page-type explicitly as it is inferred from the action.

    Parameters

    • productId: string
    • skuId: string

    Returns Action

    the action instance to load content or track events.

    nostojs(api => api
    .defaultSession()
    .viewProductSku("101", "101-sku-1")
    .setCategories(["/men/trousers"])
    .setRef("123", "example_reco_id")
    .setPlacements(["cross-seller"])
    .load()
    .then(data => console.log(data)))
  • Create a new action for a search page. This should be used only on search pages. A search page action requires you to pass the search term. For example, if the user search for "black shoes", you must pass in "black shoes" and not an encoded version such as "black+shoes".

    You must invoke [the load method]Action#load on the resultant action in order for the request to be made.

    You do not need to specify the page-type explicitly as it is inferred from the action. Search terms are not case-sensitive.

    Parameters

    • ...searchTerms: string[]

      the non-encoded search terms

    Returns Action

    the action instance to load content or track events.

    nostojs(api => api
    .defaultSession()
    .viewSearch("black shoes")
    .load()
    .then(data => console.log(data)))
  • Create a new action for a tag page. This should be used only on tag pages.

    You must invoke [the load method]Action#load on the resultant action in order for the request to be made.

    You do not need to specify the page-type explicitly as it is inferred from the action. Note: tags are not case-sensitive.

    Parameters

    • ...tags: string[]

      the set of the tags being viewed.

    Returns Action

    the action instance to load content or track events.

    as this is an advanced action with a limited a use case

    nostojs(api => api
    .defaultSession()
    .viewTag("colourful")
    .load()
    .then(data => console.log(data)))