Api

Api

new Api()

Methods

addSegmentCodeToVisit(segment) → {Promise.<Object>}

API method to manually add a given segment code to the the current user. This is used in situations when you want to segment users based on external logic.

Sending a segment code does not automatically create the corresponding segment.
Parameters:
Name Type Description
segment String
Example

to add a user to segment when they've used a discount code

nostojs(api => api.addSegmentCodeToVisit('discount code user'))

createRecommendationRequest(flags) → {RequestBuilder}

API method to create a recommendation request. This should only be used when you require programmatic access to the Nosto request builder.

If your site is a single-page application atop a framework such as React, Vue, Angular or the likes, and you are implementing Nosto, you must use the Api#defaultSession method.
Parameters:
Name Type Description
flags Object a set of flags to customise to request behaviour (eg. {"includeTagging":true} to initialise the request from the page tagging.

customer(customer) → {Promise.<Object>}

API method to resend the provided customer details to Nosto. This is used in situations when the customer details is loaded after the client script initialization.

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.

It is not recommended to pass the current customer details to the request builder but rather use the customer tagging.

If your site is a single-page application atop a framework such as React, Vue, Angular or the likes, and you are implementing Nosto using the Session API, you do not ever need this method. Nosto implementations on the single-page applications don't rely on the tagging metadata and therefore, usage of this method is indicative of an incorrect usage pattern. You should be using the Session API @see Session#setCustomer to provide the customer information.

This method is legacy method and therefore named incorrectly. Is the customer equivalent of the resendCartContent method and actually should be called resendCustomerDetails.
Parameters:
Name Type Description
customer Customer the details of the currently logged in customer
To Do:
  • deprecate this method and rename it to resendCustomerDetails
Example
nostojs(api => api.customer({
  first_name: "Mridang",
  last_name: "Agarwalla",
  email: "mridang@nosto.com",
  newsletter: false,
  customer_reference: "5e3d4a9c-cf58-11ea-87d0-0242ac130003"
}))

defaultSession() → {Session}

API method to access the default session. This should only be used when implementing Nosto on a single-page application atop a framework such as React, Vue, Angular or the likes.

If you are not using a single-page application but require programmatic access to the Nosto request builder use Api#createRecommendationRequest.

listen(phase, cb)

API method to register a listener for JS API events. Nosto's JS API dispatches multiple events across the session lifetime.

Due to the wide gamut of events dispatched, listing them all is still a work in progress.
Parameters:
Name Type Description
phase String
cb function the callback function to be invoked
Example

to log a message whenever a request is made to Nosto

nostojs(api => api.listen('taggingsent'), () => console.log("The tagging was sent"));

loadRecommendations() → {Promise}

API method to reload all onsite recommendations and content. This function is automatically invoked when the page loads. This should only be used when need to reload all recommendations and content e.g. on a overlay modal.

Incorrect or extraneous usage of this method will lead to skewed page-view statistics, ad every invocation of this method results in a +1 page-view count.
To Do:
  • rename the method to be more in line with the behaviour
Example

to manually load recommendations after DOM ready

nostojs(api => api.loadRecommendations());

pageTagging() → {Object}

API method that to debug the state the page tagging. This is useful for debugging what Nosto sees. You are able to see all the page tagging via the debug toolbar.

If your site is a single-page application atop a framework such as React, Vue, Angular or the likes, and you are implementing Nosto using the Session API, you do not ever need this method. Nosto implementations on the single-page applications don't rely on the tagging metadata and therefore, if used, this method will always return an empty object (as there shouldn't be any tagging/metadata on the page).

This is only for debugging purposes and should never be used in a production environment
Example

to log the page state to the console

nostojs(api => console.log(api.pageTagging()));

recommendedProductAddedToCart(productId, nostoElementId) → {Promise.<Object>}

Parameters:
Name Type Description
productId String
nostoElementId String

reportAddToCart(cartItemId, nostoElementId) → {Promise.<Object>}

Parameters:
Name Type Description
cartItemId
nostoElementId

resendCartContent(cart) → {Promise.<Object>}

API method to resend the cart content to Nosto. This is used in situations when the cart tagging is loaded after the client script initialization.

If your site is a single-page application atop a framework such as React, Vue, Angular or the likes, and you are implementing Nosto using the Session API, you do not ever need this method. Nosto implementations on the single-page applications don't rely on the tagging metadata and therefore, usage of this method is indicative of an incorrect usage pattern. You should be using the Session API @see Session#setCart to provide the cart information.
Parameters:
Name Type Description
cart Cart content of the cart
Example
nostojs(api => api.resendCartContent({
  items: [
    product_id: "101",
    sku_id: "101-S",
    name: "Shoe",
    unit_price: 34.99
    price_currency_code: "EUR"
  ]
}))

resendCartTagging() → {Promise.<Object>}

API method to resend the cart tagging to Nosto. This is used in situations when the cart tagging is loaded after the client script initialization. This method reads all metadata having the class "nosto_cart" and sends the extracted cart information to Nosto.

If your site is a single-page application atop a framework such as React, Vue, Angular or the likes, and you are implementing Nosto using the Session API, you do not ever need this method. Nosto implementations on the single-page applications don't rely on the tagging metadata and therefore, usage of this method is indicative of an incorrect usage pattern. You should be using the Session API @see Session#setCart to provide the cart information.
Example
nostojs(api => api.resendCartTagging())

resendCustomerTagging() → {Promise.<Object>}

API method to resend the customer tagging to Nosto. This is used in situations when the customer tagging is loaded after the client script initialization. This method reads all metadata having the class "nosto_customer" and sends the extracted customer information to Nosto.

If your site is a single-page application atop a framework such as React, Vue, Angular or the likes, and you are implementing Nosto using the Session API, you do not ever need this method. Nosto implementations on the single-page applications don't rely on the tagging metadata and therefore, usage of this method is indicative of an incorrect usage pattern. You should be using the Session API @see Session#setCustomer to provide the customer information.
Example
nostojs(api => api.resendCustomerTagging())

sendTagging(ref, opts) → {Promise.<Object>}

API method to resend all the tagging to Nosto. This is used in situations when the cart and the customer tagging is loaded after the client script initialization. While you can use resendCartTagging and the resendCustomerTagging to achieve the same - this method will make a single request to Nosto.

If your site is a single-page application atop a framework such as React, Vue, Angular or the likes, and you are implementing Nosto using the Session API, you do not ever need this method. Nosto implementations on the single-page applications don't rely on the tagging metadata and therefore, usage of this method is indicative of an incorrect usage pattern.
Parameters:
Name Type Description
ref
opts
Example
nostojs(api => api.sendTagging())

setAutoLoad(flag)

API method to disable the automatic initialization of Nosto. This should be used in cases when you want to manually load content.

If your site is a single-page application atop a framework such as React, Vue, Angular or the likes, and you are implementing Nosto using the Session API, you must disable auto-loading.
Parameters:
Name Type Description
flag Boolean A true or false value indicating whether to automatically load or not
Example
nostojs(api => api.setAutoLoad(false))
nostojs(api => api.loadRecommendations())

setRecommendationsEnabled(flag)

Parameters:
Name Type Description
flag Boolean A true or false value indicating whether to disable placements or not
Deprecated:
  • Yes