When To Call Functions

There is sometimes confusion around when to call each of the functions in the Contextual Commerce SDK. This page will focus explicitly on when each function should be called.

activate(...)

As the initiating function of the Contextual Commerce system, this function should be called only once, and when your app starts. Be sure to register all providers and subscribe to all events before calling this function. This function should be called before the app renders the first page.

subscribeGroupAssigned(...)

Call this function only once, immediately before you activate Contextual Commerce.

trackCart(...)

Start by calling this function immediately after activate(), when the app starts.

Then, every time the shopper makes a change to the cart in any way, this function must be called. This includes adding, removing, modifying and/or changing the quantities of products in the cart.

If your app has been inactive for a time, and is later reactivated, it is possible that the shopper's cart has been changed in another app / device in the interim. For this reason, trackCart should be called each time the app regains focus.

trackOrder(...)

Call this function once an order is finalized and paid. Do not call this when the shopper starts the checkout process - this function is intended only to record completed and finalized orders.

trackProduct(...)

Every time a new page is a product page, this function must be called. Call this immediately after the trackPageView function.

If your app has been inactive for a time, and is then reactivated, Contextual Commerce's session may have timed out, so you will need to call this function again with the same information. It is safe to do this whenever the app regains focus.

See Tracking Products for more information.

setCurrentProduct(...)

When the shopper is on a product page, this function must be called every time a new variation of the product is selected, including the initial variation.

Call this function immediately after the trackProduct call, plus whenever a new variant of the product is selected.

If your app has been inactive for a time, and is then reactivated, Contextual Commerce's session may have timed out, so you will need to call this function again with the same information. It is safe to do this whenever the app regains focus.

See Tracking Products for more information.

<TrackPageView pageIdentifier={...} navigator={...}/>

Embed this component in all pages. This includes the home page, all collection and product pages, but also every other page in the site.

See Tracking Pages for more information, and a list of supported page types.

setRestrictionState(...)

After activation, on any page in your app, if, during the operation of the page, a popup or slide-over appears, or for any other reason the page is in a state in which you do not want a campaign to appear, you can call this function to set the Restriction state to .RESTRICTED. This will prevent any campaigns appearing over the page.

Remember to call the function again to set the state to .UNRESTRICTED when you are ready to see campaigns again. The state remains in effect across new page loads, so if you set the state to .RESTRICTED and forget to reset it, no more campaigns will show for that session.

showCampaign(...)

This function is used exclusively for testing. See Testing for details, but essentially you will have added a section in your preferences or settings that allows a developer-user to supply the ID of a concept (campaign). When that happens, you would call this function with that ID.

This function should not be called in any other circumstances. It is not required to make a campaign appear for a shopper under normal circumstances. This is exclusively used to force our Contextual Commerce Brain to show a campaign that would not normally be allowed to show, in order to test it, and ensure that it displays correctly, and does not adversely affect your app.