SPA (Single Page Application)
Overview
Introduction
This page explains how to integrate Taboola recommendations into an SPA (Single Page Application), using the param values provided by Taboola.
Prefilled scriptsIf you received prefilled scripts from Taboola, you can copy and paste them 'as is', instead of editing param values.
New integration?If this is a new integration, please provide Taboola with a timeframe for launch, so we can plan post-launch QA.
Terminology & flow
TaboolaloaderLoads your publisher-specific configurations and fetches your Taboola recommendations.
In order not to impact page load time,
loader.jsis loaded asynchronously.
The_taboolacommand queueA global array whose existence is ensured by the following code:
window._taboola = window._taboola || [];You communicate with the Taboolaloader by 'pushing' commands to this queue.
This allows you to pass commands 'safely', regardless of whether the Taboolaloader has loaded yet.
General flowThe initial page
- Add the Taboolaloader <script> to the page <head>.
- Pass the page details to the
_taboolacommand queue.- For each placement on the page:
- Create a
<div>container with a unique ID.- Pass the placement details to the
_taboolacommand queue.- Pass a
flushcommand.Each new page
- Pass a
newPageLoadnotification. *- Pass the page details to the
_taboolacommand queue.- For each placement on the page:
- Create a
<div>container with a unique ID.- Pass the placement details to the
_taboolacommand queue.- Pass a
flushcommand.(* The
newPageLoadnotification is the key difference between SPA and Infinite Scroll integrations.)
Initial page vs subsequent pagesThe integration steps are similar - but not identical - for both the initial page and subsequent pages.
Supported browsers
Taboola's SPA integration is supported for any browser that is fully compatible with ES5 (ECMAScript 5) or higher. This includes all modern web browsers.
Older browsers (such as IE 11) may encounter problems with video ads.
The initial page
Add the Taboola loader
Add the Taboolaloader script to the page <head>.
The Taboolaloader should run once only, when the initial page is loaded.
Fill in your own Publisher ID, as provided by Taboola (or use the prefilled script that was sent to you).
<script>
// *Global* command queue for the page
window._taboola = window._taboola || [];
!function (e, f, u, i) {
if (!document.getElementById(i)) {
e.async = 1;
e.src = u;
e.id = i;
f.parentNode.insertBefore(e, f);
}
// Fill in your Publisher ID (an alphabetic string), as provided by Taboola:
}(document.createElement('script'), document.getElementsByTagName('script')[0], '//cdn.taboola.com/libtrc/<publisher-id>/loader.js', 'tb_loader_script');
if (window.performance && typeof window.performance.mark == 'function') {
window.performance.mark('tbl_ic');
}
</script>Param | Instructions |
|---|---|
| Fill in your Publisher ID (an alphabetic string), as provided by Taboola. For example, if your Publisher ID is
|
Publisher IDYour Publisher ID is a unique, alphabetic String, provided by Taboola.
It might contain dashes - e.g.
"{user.publisherID}"- but not spaces.
Pass the page details
Pass the page details to the Taboola command queue:
// *Global* command queue for the page
window._taboola = window._taboola || [];
// Pass the *page* details to the command queue:
_taboola.push({<page-type>:'auto', url:'<url>'});Param | Instructions | Notes |
|---|---|---|
| Replace this with the relevant page type (as provided by Taboola) - e.g.
| Possible values: |
| To obtain an automated ID, leave this param as is: Else, insert your own, internal ID: | Specifies the ID of this page, for the given page type. |
| Pass the fully-qualified, canonical URL of the initial page. |
Guidelines
- Use your own param values, as provided by Taboola.
- Pass the fully-qualified, canonical URL of the initial page.
- If you copy the above code snippet to another page, make sure that
page_typecorrectly reflects the new page type.
Pass the placement details
For each placement on the page:
- Insert a
<div>container, in the location that the placement should display:
<div id="<container>"></div>Assign the
<div>a unique ID for that page - e.g."{user.containerId}".Tip: Consider using an ID that is similar to the placement name.
For example, if the placement is:
'{user.placementName'}'
Then you might use an ID of:'{user.containerId}'
- Pass the placement details to the Taboola command queue:
// *Global* command queue for the page
window._taboola = window._taboola || [];
// For each placement, pass *your* param values, as provided by Taboola:
_taboola.push({mode: '<mode>', container: '<container>', placement: '<placement>', target_type: '<target_type>'});
ParamsThe values shown below are sample values only. Make sure to fill in your own values, as provided by Taboola.
mode- The UI Mode ID for this placement, as provided by Taboola:E.g.
'{user.mode}'container- The ID of the<div>container for this placement's content:E.g.
'{user.containerId}'
Must be unique per pageplacement- The placement name, as provided by Taboola:E.g.
'{user.placementName}'
Must be unique per pagetarget_type- The target type, as provided by Taboola:E.g.
'{user.targetType}'
Guidelines
- Use your own param values, as provided by Taboola.
- Do not update
target_type, unless your Taboola Account Manager instructs you to do so.
Custom SegmentsTo target a custom segment - or simply track its performance - you can optionally pass a
csegparam.For more information, see Custom Segments - or reach out to your Taboola Account Manager.
Flush the queue
Immediately after passing details for the last placement, pass the flush command:
// *Global* command queue for the page
window._taboola = window._taboola || [];
// Pass the `flush` command:
_taboola.push({flush: true});
- The
flushcommand instructs Taboola to fetch any placements in the command queue (and not wait any further).* Pass theflushcommand once only - after passing all placements for that page.* If you have unique requirements, reach out to Taboola to discuss alternative flows.
Need a hand?Feel free to reach out on our Community Discussion page!
A subsequent page
Pass a new page notification
When a subsequent page is loaded dynamically, pass a newPageLoad notification to the Taboola command queue. This notifies Taboola to reset the page state, allowing you to submit new page and placement details.
This step is required for subsequent pages only - not the initial page.
// *Global* command queue for the page
window._taboola = window._taboola || [];
// Submit a `newPageLoad` notification to the command queue:
// ONLY for subsequent pages (not the initial page)
_taboola.push({notify:'newPageLoad'});
Handling theBackbuttonWhen handling the
Backbutton, note the following best practice:
- Do not use the
Backbutton to trigger anewPageLoadnotification.Instead, submit it when the page renders, together with the Taboola placement scripts.
- If the above approach is not feasible, then make sure to trigger the
newPageLoadnotification together with the Taboola placement scripts.Submitting
newPageLoadon its own will cause TaboolaExplore More to fail.For more information, reach out to your Taboola Account Manager.
Pass the page details
Pass the page details to the Taboola command queue:
// *Global* command queue for the page
window._taboola = window._taboola || [];
// Pass the *page* details to the command queue:
_taboola.push({<page-type>:'auto', url:'<url>'});Param | Instructions | Notes |
|---|---|---|
| Replace this with the relevant page type (as provided by Taboola) - e.g.
| Possible values: |
| To obtain an automated ID, leave this param as is: Else, insert your own, internal ID: | Specifies the ID of this page, for the given page type. |
| Pass the fully-qualified, canonical URL of the new page. |
Guidelines
- Use your own param values, as provided by Taboola.
- Pass the fully-qualified, canonical URL of the initial page.
- If you copy the above code snippet to another page, make sure that
page_typecorrectly reflects the new page type.
Pass the placement details
For each placement on the page:
- Insert a
<div>container, in the location that the placement should display:
<div id="<container>"></div>Assign the
<div>a unique ID for that page - e.g."{user.containerId}".Tip: Consider using an ID that is similar to the placement name.
For example, if the placement is:
'{user.placementName}'
Then you might use an ID of:'{user.containerId}'
- Pass the placement details to the Taboola command queue:
// *Global* command queue for the page
window._taboola = window._taboola || [];
// For each placement, pass *your* param values, as provided by Taboola:
_taboola.push({mode: '<mode>', container: '<container>', placement: '<placement>', target_type: '<target_type>'});
ParamsThe values shown below are sample values only. Make sure to fill in your own values, as provided by Taboola.
mode- The UI Mode ID for this placement, as provided by Taboola:E.g.
'{user.mode}'container- The ID of the<div>container for this placement's content:E.g.
'{user.containerId}'
Must be unique per pageplacement- The placement name, as provided by Taboola:E.g.
'{user.placementName}'
Must be unique per pagetarget_type- The target type, as provided by Taboola:E.g.
'{user.targetType}'
Guidelines
- Use your own param values, as provided by Taboola.
- Do not update
target_type, unless your Taboola Account Manager instructs you to do so.
Custom SegmentsTo target a custom segment - or simply track its performance - you can optionally pass a
csegparam.For more information, see Custom Segments - or reach out to your Taboola Account Manager.
Flush the queue
Immediately after passing details for the last placement, pass the flush command:
// *Global* command queue for the page
window._taboola = window._taboola || [];
// Pass the `flush` command:
_taboola.push({flush: true});
- The
flushcommand instructs Taboola to fetch any placements in the command queue (and not wait any further).* Pass theflushcommand once only - after passing all placements for that page.* If you have unique requirements, reach out to Taboola to discuss alternative flows.
Need a hand?Feel free to reach out on our Community Discussion page!
What's Next?
- In order to provide personalized recommendations for impressions in the EU and California, make sure to implement GDPR and CCPA respectively.
- If you have not already done so, configure ads.txt.
- Contact Taboola so that we can verify your implementation.
Updated 25 days ago
