Ad unit code features

You can enable additional functions and change certain settings by directly editing the code of the ad unit.

The Ya.Context.AdvManager.render function is responsible for invoking the ad in the code.

Alert

  • If you do not have sufficient experience working with ad tags, we don't recommend making changes to the ad unit tag. Changes to any variables or functions other than those described on this page might lead to errors in Yandex Advertising Network ad units and cause you to lose revenue.

  • You can't use the ad unit code as the alternative code in other advertising systems.

Statistical cross-sections

You can also specify sample IDs in the unit code and collect separate statistics for each sample. A sample ID can be set using a dedicated variable:

Variable name Description Data type
statId Sample ID A number between 1 and 1000000000

Enter the sample ID in the ad tag to be placed on the website. For example:

Ya.Context.AdvManager.render({
    blockId: "R-A-123456-1",
    renderTo: "yandex_rtb_R-A-123456-1",
    statId: 34567, // CROSS SECTION ID
});

For the R-A-123456-1 unit from the example, the sample ID is 34567. It can also be specified in other units. This will allow you to view group statistics for these units. By placing the R-A-123456-1 unit on other pages, you can specify a different sample ID to view statistics separately for each placement.

Limiting banner height

By default, Adaptive  Text & Image ads completely fill their container. You can adjust the unit height using the height and max-height parameters.

Recommended values for the maximum unit height:

  • Minimum of 70 and maximum of 90 vh for the mobile version.

  • Minimum of 40 and maximum of 60 vh for the desktop version when placed on side panels. An example of a code unit with a maximum height:

<div id="yandex_rtb_R-I-677833-1" style="max-height: 80vh;">
  <div style="height: 72vh;"></div>
</div>
<script>
  window.yaContextCb.push(()=>{
    Ya.Context.AdvManager.render({
      renderTo: 'yandex_rtb_R-I-677833-1',
      blockId: 'R-I-677833-1'
    })
  })
</script>

Setting up a theme

You can set the theme (light or dark) in the Yandex Advertising Network interface when creating or editing a unit. You can also control the unit theme via the embed code using the darkTheme parameter:

  • darkTheme: false: The light theme is applied.

  • darkTheme: true: The dark theme is applied. Units in the dark theme differ not only in the background, but also in the color of the text, border, buttons, and other ad elements.

When the darkTheme parameter is added, the theme settings set in the interface are ignored.

Managing the unit theme through the code is convenient for websites with a dynamic theme. For example, you can use JavaScript to set different values of the darkTheme parameter: depending on the time of day, the website or browser theme selected by the user.

Sample code of an ad unit with a dark theme:

Ya.Context.AdvManager.render({
    blockId: "R-A-123456-1",
    renderTo: "yandex_rtb_R-A-123456-1",
    darkTheme: true
});
Example of a Top Ad unit in the dark theme

Placing a Floor Ad or Top Ad simultaneously with a Feed

You can't display a Feed, a Floor Ad, and a Top Ad on the same screen. Configure the observer callback function to delete your Floor Ad or Top Ad unit when the Feed becomes visible.

Example of an ad tag with the observer function

<div id="yandex_rtb_R-A-588461-101"></div>
<script>
  window.yaContextCb.push(() => {
          Ya.Context.AdvManager.renderFeed({
                  blockId: 'R-A-588461-101',
                  renderTo: 'yandex_rtb_R-A-588461-101'
          })
  })
  window.yaContextCb.push(() => {
          Ya.Context.AdvManager.render({
                  "blockId": 'R-A-588461-98',
                  "type": "ad unit type", // topAd or floorAd
                  "platform": "touch"
          })
  })
  window.yaContextCb.push(() => {
          Ya.Context.AdvManager.render({
                  "blockId": "R-A-588461-109",
                  "type": "ad unit type", // topAd or floorAd
                  "platform": "desktop"
          })
  })
  const feed = document.getElementById('yandex_rtb_R-A-588461-101'); // ID from the renderTo field
  const callback = (entries) => {
          entries.forEach((entry) => {
                  if (entry.isIntersecting) {
                          Ya.Context.AdvManager.destroy({
                                          blockId: 'R-A-588461-98'
                                  }), // blockId touch
                                  Ya.Context.AdvManager.destroy({
                                          blockId: 'R-A-588461-109'
                                  }); // blockId desktop
                  }
          });
  };
  const observer = new IntersectionObserver(callback, {
          threshold: 0,
  });
  observer.observe(feed);
</script>

Callback functions in the ad tag

You can add the following callback functions to the ad tag:

onClose

To find out at what point the ad was closed, add the onClose callback function when placing the unit. This function is available for interstitial, Top Ad, and Floor Ad units.

You can set which action to call after the banner is closed based on your business logic. For example, this action may be to call another ad unit. To do this, enter the code with the action in onClose.

Example of an ad tag with the onClose function
<!-- Yandex.RTB R-A-588461-68 -->
<script>window.yaContextCb.push(()=>{
  Ya.Context.AdvManager.render({
    type: 'ad unit type', // fullscreen, topAd, or floorAd
    blockId: 'R-A-588461-68',
    onClose: () => {
        // action performed by the website after the banner is closed     }
  })
})</script

onError

The onError callback function is called if an error occurred during request execution. When called, it sends the following parameters:

  • type: Error type. It may take the following values:

    1. error: Couldn't load the ad unit.
    2. warning: The ad unit loaded with an error.
  • code: Error code.

  • text: Text description of the error.

Possible error codes and their values

Type of error

Error code

What it means

How to fix it

error

DESKTOP_FULLSCREEN

An attempt was made to load an interstitial mobile unit on the desktop version of the site.

You can only use interstitial ad units for the mobile version of the website.

error

CONTAINER_NOT_FOUND

Couldn't find the container to load the ad.

Failed to find the container when loading the code or after receiving ad data from the server. This error usually occurs on pages with dynamic content.

error

BLOCK_NOT_FOUND

The ad was called, but the ad unit with the specified ID wasn't found.

Check the ad tag obtained in the Yandex Advertising Network interface. Make sure that the unit ID is correct.

error

PAGE_NOT_FOUND

The ad was called, but the website with the specified ID wasn't found.

Check the ad tag obtained in the Yandex Advertising Network interface. Make sure that the unit ID is correct.

error

WRONG_DOMAIN

The domain that called the ad isn't specified in the website settings.

Make sure that the domain you placed the ad unit on is specified in the website settings.

If the domain isn't specified, add a new website with this domain and create an ad unit for this website. You can add mirrors to existing websites.

error

PAGE_DISABLED

The ad was called, but the specified website isn't working.

Check the website status in the Yandex Advertising Network interface. The website can be stopped, archived, or blocked for violating the Rules of participation in Yandex Advertising Network.

error

UNKNOWN_BLOCK_TYPE

An attempt was made to load an unknown unit type.

Check the ad tag in the Yandex Advertising Network interface. Unit ID must start with the letters R, D, or Y.

error

TOO_SMALL_CONTAINER

None of the formats supported by the unit fit the ad container.

Check the size of the container for loading ads. The minimum container size is 160 x 50 pixels.

error

VIDEO_ERROR

An error occurred when loading the video ad.

Check the size of the container for loading ads. The minimum size is 320 x 180 pixels.

warning

CONTAINER_IS_HIDDEN

An attempt was made to load an ad into a 0 x 0 pixel container (for example, with the display:none style).

Hidden containers slow down ad loading. We recommend inserting ad units into visible containers.

An example of code that is used to invoke an ad with the onError function
Ya.Context.AdvManager.render({
    blockId: 'R-I-106712-1',
    renderTo: 'block_container',
    onError: (data) => {
        console.log('type', data.type); // Error type: error or warning
        console.log('code', data.code); // Error code (see description above)
        console.log('text', data.text); // Text description of the error
                                                  // Error processing by the website     }
});

onRender

The onRender callback function is used to obtain information about whether the ad was rendered for the client and which particular ad was shown. The function accepts the data.product parameter with one of the following values:

  • direct: The unit was showing a Yandex Direct ad.

  • rtb: The unit was showing a display advertising.

The onRender callback function must be added as the last parameter in the Ya.Context.AdvManager.render function.

An example of code that is used to call an ad with the onRender function
Ya.Context.AdvManager.render({
    blockId: "R-A-123456-1",
    renderTo: "yandex_rtb_R-A-123456-1",
    onRender: (data) => { console.log(data.product); }
})

Running your own ads in an ad unit

If the auction doesn't produce a suitable offer for your ad unit, you can use it to run your own ads. To do this, add your ad tag to the unit code.

  1. Get the unit code in the Yandex Advertising Network interface (in the unit settings, click Get code → Copy to clipboard).

  2. Place the ad tag on your website.

  3. Include your ad tag in the last parameter of the Ya.Context.AdvManager.render function in the unit code.

Example of code that inserts your own ad
Ya.Context.AdvManager.render({
    blockId: 'R-A-123456-1',
    renderTo: 'yandex_rtb_R-A-123456-1',
}, () => {
    // INSERT YOUR AD CODE HERE
})

If the auction doesn't produce a suitable offer, the alternative code is executed, and your own ad is displayed instead of the ad unit (not via iframe). Impressions of your own ads don't count towards the Yandex Advertising Network statistics.

Note

Yandex Advertising Network units created from Adfox cannot have a custom ad code. If no suitable Yandex Advertising Network ad was picked for this unit, the request is returned to Adfox and the right to serve ads passes to other demand platforms or custom promo campaigns. Learn more about selecting Adfox ads.

Ad impressions in infinite scroll feeds

You can display ads in infinite scroll feeds using the same ad unit by changing the name of the renderTo container. The unique blockId from the code designer remains unchanged.

Example of an ad tag for the first ad unit
<div id="yandex_rtb_R-A-123456-1-1"></div>
<script>window.yaContextCb.push(()=>{
    Ya.Context.AdvManager.render({
        blockId: "R-A-123456-1",
        renderTo: "yandex_rtb_R-A-123456-1-1", // at the end of the name you need to put 1
    })
})</script>
Example of the ad tag for the second ad unit
<div id="yandex_rtb_R-A-123456-1-2"></div>
<script>window.yaContextCb.push(()=>{
    Ya.Context.AdvManager.render({
        blockId: "R-A-123456-1",
        renderTo: "yandex_rtb_R-A-123456-1-2", // at the end of the name you need to put 2
    })
})</script>

Experiments on ads with Varioqub

You can conduct experiments on ads placed on your site using Varioqub. This tool lets you create an experiment with multiple options and both change the elements and layout of the site and compare different ad units and their settings. As a result of your experiment, you get statistics for indicators that help determine which option is most effective in terms of monetization and user metrics. For more information, see Varioqub experiments in Help.

Contact support

If you didn't find the information you were looking for, check the FAQ or contact us.

I inserted code but the ads are not being displayed

There are several reasons why problems displaying ads may occur:

  • The ad tag has recently been installed. The Yandex Advertising Network robot requires some time to index the page and link it to ads.

  • The ad tag is incorrect. Make sure that your JavaScript ad unit tag has been installed correctly. Don't change the tag when copying it. When copying the tag, your text editor might truncate or wrap long strings, which might cause the JavaScript code to run incorrectly.

  • Tag invalid for the website. Make sure that the inserted ad unit tag was created for this website and not for one of your other websites.

  • Website is blocked from indexing. The site must be accessible for indexing by the Yandex Advertising Network robot, YandexDirect . If indexing a site or its individual pages is banned (for example, in the robots.txt file), the robot can't index the content of the pages or select relevant ads for them.

  • Text is wrapped in the <noindex> tag. Make sure that the page has text on it and that it has not been excluded using the <noindex> tag. You can't select theme-based ads for pages that have no text.

  • The information in the browser cache is old. Refresh the page by pressing CTRL + F5 (Windows) or Command + R (Mac).

Write to WhatsApp Go to WhatsApp

Write to chat

Send an email



You can also go to