Shopping Tab

This page assumes that:

Integrating the Search Expander shopping results is similar to integrating the standard widgets.

The shopping widget is expected to be rendered on a separate tab or page from your web search results. On this shopping route, you will need to do the following:

  • Add some markup to your shopping results page
  • Load the Search Expander JavaScript library on the page
  • Call the global sxpr.shopping() JavaScript function
Important: Please check out our list of supported markets!

Markup

You must provide an HTML block element somewhere in your shopping route's content with the attribute data-sxpr-shopping. This container will have the following elements rendered inside it:

  • Mainline product links
  • A carousel of sponsored product links above the mainline products
  • An optional carousel of eco-friendly product links above the mainline products
  • Inputs for filtering and sorting the products
  • A tree of product categories
  • Pagination buttons for the mainline products

The shopping widget is mobile-friendly, but its container should be placed in a layout context which allows for a sizeable amount of content (e.g. a sidebar would be inappropriate).

Load the Search Expander library

You can include the Search Expander JavaScript library using:

<script src="https://cdn.searchexpander.com/js/sxpr.js"></script>

Call sxpr.shopping()

To run Search Expander, call sxpr.shopping() after the script has loaded:

sxpr.shopping({
    se: 'your-search-engine-id', // Replace 'your-search-engine-id' with your search engine's ID
    // ... other settings here
});

Search query

Search Expander needs to know your user's search query in order to provide relevant shopping results. You can provide the search query in one of these ways:

  • Pass a q value to sxpr.shopping() (this is the most direct method)
  • Add the data-sxpr-input attribute to a search input whose value is the search query
  • Pass a searchInput value to sxpr.shopping() which references a search input whose value is the search query
  • Pass a shoppingUrlTemplate string to sxpr() and sxpr.shopping() (see below)

JavaScript example

sxpr.shopping({
    se: 'your-search-engine-id',
    q: 'lorem ipsum',
    // Or alternatively:
    searchInput: document.getElementById('my-search-input'),
});

HTML example

<input name="q" value="lorem ipsum" data-sxpr-input>

Empty search queries

Unlike the other Search Expander widgets, the shopping widget will be functional even without a user search query. If no search query is provided and cannot be derived from the page's search inputs or URL, the widget will render without any product links but will provide a tree of product categories in a sidebar. Users can click on these categories in order to initiate product searches.

Linking to your shopping results

The Search Expander widgets for general web search results will sometimes contain links to your shopping results. In order that these links are pointing to the right place, Search Expander needs to know how to construct their URLs. To allow this, you must provide a shoppingUrlTemplate setting when calling sxpr() on your general web search results page.

For example, let's say your shopping results appear under the URL:

https://example.com/shopping

In this case, you should set shoppingUrlTemplate to:

https://example.com/shopping?q={searchTerms}.

(Note that the URL parameter name q in the above example can be anything you like.)

Relative URLs with absolute paths are allowed, for example:

/shopping?q={searchTerms}

// Run on your web results page:
sxpr({
    se: 'your-search-engine-id',
    shoppingUrlTemplate: 'https://example.com/shopping?q={searchTerms}',
    // ... other settings here
});

The {searchTerms} tag will be automatically replaced with the user search query, which will be sent to the Search Expander API to obtain relevant products.

(Passing shoppingUrlTemplate to sxpr.shopping() in addition to the above is not strictly necessary but can help Search Expander figure out the user search query if q is not set.)

Dynamic control of shopping route links

If navigating to the shopping route on your site is initiated by JavaScript code rather than standard page navigation (e.g. in a SPA), you may need to use JavaScript to handle clicks on shopping route links, rather than relying on normal links. You can do this by setting a handleShoppingClick function on the sxpr() settings object. This function can take care of updating the page so that the user is navigated to the shopping results.

Note that handleShoppingClick will need to be passed to sxpr() on your web results route, rather than to sxpr.shopping() on the shopping route.

If you provide a handleShoppingClick function, then when a widget shopping route link is clicked, the link's default behaviour will be overridden and the function will be left to handle the shopping results request.

handleShoppingClick is passed an object with the following interface:

{
    q: string;                  // The shopping search query
    origQ: string;              // The original user query (which may not be the same as q)
    link: HTMLElement;          // The clicked link or alert button
    event: MouseEvent;          // The link's click event
    url: string | undefined;    // The shopping route's URL (only set if shoppingUrlTemplate is set)
}

You will usually only need to use the q value in this object.

Shopping route alert button

In order to alert users to the shopping route when they carry out an ad-friendly web search, you can add an alert button to your web results route. This can just be a div or span element with the attribute data-sxpr-shop-alert. For example:

<span data-sxpr-shop-alert>Shopping</span>

Clicking on this element will take the user to your shopping route, in accordance with the shoppingUrlTemplate setting. If handleShoppingClick is set, this function will be called in place of direct navigation.

Styling and behaviour will be taken care of by the Search Expander library, but of course you can tweak its appearance using CSS.

If you want the alert button hidden until shopping results are ready, add the following CSS to your page:

[data-sxpr-shop-alert] {
    display: none;
}

…and Search Expander will reveal it at the appropriate time.

Sponsored Products carousel

By default, the shopping page will display a carousel of sponsored product thumbs above the other results. If you want to disable this carousel, you must set enableSponsoredProducts to false on the object passed to sxpr.shopping(). For example:

// Run on your web results page:
sxpr.shopping({
    se: 'your-search-engine-id',
    q: 'lorem ipsum',
    
    // Add this setting to remove the Sponsored Products carousel:
    enableSponsoredProducts: false,

    // ... other settings here
});

Eco-friendly Products carousel

In addition to the Sponsored Products carousel, you have the option of adding another carousel containing a selection of eco-friendly products, where these are available. In order to enable this carousel, you must set enableEcoProducts to true on the object passed to sxpr.shopping(). For example:

// Run on your web results page:
sxpr.shopping({
    se: 'your-search-engine-id',
    q: 'lorem ipsum',
    
    // Add this setting to get a carousel of eco-friendly products:
    enableEcoProducts: true,

    // ... other settings here
});

Shopping-related settings reference

Settings for sxpr() (web search)

PropertyDescriptionType
enableWebProducts

This setting enables product ads in either the top bar as a carousel or in the knowledge panel.

Set to true or 'topBar' to enable product ads in a top bar carousel, or to 'knowledgePanel' to enable product ads in a section of the knowledge panel, or false to disable. (Default false.)

boolean | 'topBar' |
'knowledgePanel'
topBarContainer

HTML container for top bar, or a selector string for obtaining it.

Alternatively, set a data-sxpr-top-bar HTML attribute on the container.

HTMLElement | string
shoppingUrlTemplate

Template for shopping route link URLs. These links will appear in widgets and navigate users to your shopping route.

This template should be passed to both sxpr() and sxpr.shopping().

It is strongly recommended that you provide this setting, unless you are using a handleShoppingClick function instead.

Example: /shopping/?q={searchTerms}

string
handleShoppingClick

Click handler for the internal shopping route links in widgets.

Used where new shopping results are loaded dynamically (using JavaScript) rather than via traditional page navigation, meaning that shoppingUrlTemplate alone is insufficient. If set, shopping route links will not be followed and this function will handle the click instead.

More info

(data: {
  q: string;
  origQ: string;
  link: HTMLElement;
  event: MouseEvent;
  url: string | undefined;
}) => void

Settings for sxpr.shopping() (shopping search)

PropertyDescriptionType
shoppingContainer

HTML container for shopping results, or a selector string for obtaining it.

Alternatively, set a data-sxpr-shopping HTML attribute on the container.

HTMLElement | string
shoppingUrlTemplate

Template for shopping route link URLs. These links will appear in widgets and navigate users to your shopping route.

This template should be passed to both sxpr() and sxpr.shopping().

It is strongly recommended that you provide this setting, unless you are using a handleShoppingClick function instead.

Example: /shopping/?q={searchTerms}

string
shoppingAutoScroll

Whether to automatically scroll the viewport to the top of the shopping widget after product thumbnails have been loaded.

Default small (i.e. auto-scrolling behaviour applies to small viewports only).

'small' | 'large' |
'always' | 'never'
enableSponsoredProducts

Whether to add a carousel of sponsored product thumbs across the top of the shopping widget. (Default true.)

boolean
enableEcoProducts

Whether to add a carousel of eco-friendly product thumbs across the top of the shopping widget. (Default false.)

boolean

» Styling the Widgets