AdEngine on SPA pages

Overview

This integration guide applies if AdEngine needs to run on Single-Page-Application (SPA for short) type of pages. When the head code is not reloaded, and only content of the page changes due to user action.

This guide is based on react.js implementations. It should fit all others, however, please take into consideration that some elements of integration might differ.

For this integration guide, we assume the example of a website with the following two ads on every page:

  • "Top Leaderboard"

    • ad-unit-name: topleaderboard

    • div-name: adngin-topleaderboard-0

  • "Sidebar_2"

    • ad-unit-name: sidebar_2

    • div-name: adngin-sidebar_2-0

Head Configuration

The head section modifications are the essential part of this integration. Without adding the AdEngine script and configuration to the head, the ads won't render. In SPA type of pages, it's very important due to the specifications of those pages where the head config is not reloaded.

To have an optimized and effective setup to bid on the site, a few code tags need to be implemented as outlined below.

AdEngine with automatic rendering

This is the easiest way to integrate AdEngine. It will load the site specific AdEngine configuration and display ads automatically in the selected div elements, when the user first enters the page.

<script data-cfasync="false" type="text/javascript"> window.snigelPubConf = { "adengine": { "activeAdUnits": ["topleaderboard","sidebar_2"] } } </script> <script data-cfasync="false" async src="https://cdn.snigelweb.com/adengine/**site.domain**/loader.js" type="text/javascript"></script>

Note:

  • Replace **site.domain** with the actual site domain

  • Place the script at the very top of the header to ensure the best performance

  • If Google Analytics is used, place the AdEngine tag after the Google Analytics tag

  • Keep the correct order of the tags: first configuration and then loading of AdEngine

Body Configuration

The page body needs to define div elements that will be filled with ads. They need to be placed where the ad should be displayed.

The div elements should reserve the maximum width and height of the displayed ad to prevent Cumulative Layout Shift (CLS). If you are using ad labeling, reserve an additional 15px of height for the label. To reserve the proper space please consult your account manager to get the exact ad sizes.

<!-- AdEngine Top Leaderboard --> <div id="adngin-topleaderboard-0"></div> <!-- AdEngine Sidebar_2 --> <div id="adngin-sidebar_2-0"></div>

Virtual DOM pitfalls

Libraries such as React work with a virtual DOM that is used to update the real DOM whenever it detects a need for it (e.g. due a state change). Unfortunately, this makes modification of the DOM from the outside a bit tricky. In general, React will not touch external modifications, unless a complete re-rendering of a component occurs. The following examples illustrate cases where this happens:

// Example 1: conditional component rendering <SomeComponent> {someCondition ? <FirstComponent/> : <SecondComponent/>} </SomeComponent>

Since the initial ad placement was empty, React will revert to this empty state in these cases. Re-auctioning of placements is required. Here's how, using functional React:

Details about startAuction command can be found here.

Testing on a staging page

It is recommended to test an AdEngine integration on a staging page first to prevent any problems and allow your account manager to review the integration before going live on production.

If you apply all steps directly on the live page without a review from your account manager, Snigel can`t be held responsible for any revenue loss.

Consent management

If you are using AdConsent to manage user consent, there are additional elements required to be compliant with privacy laws.

Please follow the Consent Management Page Integration Guide