Working with Direct ad block code

Creating new Yandex.Direct blocks is currently not supported. Use strategies to create RTB blocks and manage displays of contextual and banner ads.

You can enable the additional functions and change certain settings by directly editing the ad invocation code without using the interface editor.

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

Attention. We do not recommend that inexperienced users make their own changes to the ad unit invocation code. Any changes to the variables and functions other than those described on this page may lead to errors in how the Advertising Network units function and, consequently, to a decrease in revenue.

The callback function that is invoked after the advertisement is rendered

The onRender Callback function allows you to obtain information about whether the ad has been rendered (whether the ad was successfully selected when the RTB system was queried) and which particular ad was shown. The function obtains the data.product parameter with one of two values:

  • direct — Yandex.Direct ads were shown in a RTB unit

  • rtb — A media ad was shown in a RTB unit.

The onRender callback function must be added as the last parameter in the Ya.Context.AdvManager.render function. The function can only be used in asynchronous code for invoking the ad.

An example of asynchronous code that is used to invoke an ad with the onRender function
function renderRtb () {
    Ya.Context.AdvManager.render({
        blockId: "R-A-12345-1",
        renderTo: 'rtb',
        async: true,
        onRender: function(data) {
            console.log(data.product);
        }
    });
};

(function(w, d, n, s, t) {
    w[n] = w[n] || [];
    w[n].push(renderRtb);
    t = d.getElementsByTagName("script")[0];
    s = d.createElement("script");
    s.type = "text/javascript";
    s.src = "http://an.yandex.ru/system/context.js";
    s.async = true;
    t.parentNode.insertBefore(s, t);
})(this, this.document, "yandexContextAsyncCallbacks");

The ad is shown in infinite scroll feeds

To display ads in infinite scroll feeds, you can use the same ad unit. You only need to change the pageNumber parameter and the name of the renderTo container. The unique blockId unit ID that can be found in the editor remains unchanged.

Example of the invocation code for the first unit
<div id="yandex_rtb_1"></div>
<script type="text/javascript">
    (function(w, d, n, s, t) {
        w[n] = w[n] || [];
        w[n].push(function() {
            Ya.Context.AdvManager.render({
                blockId: "R-A-12345-1",
                renderTo: "yandex_rtb_1",
                async: true,
                pageNumber: 1
            });
        });
        t = d.getElementsByTagName("script")[0];
        s = d.createElement("script");
        s.type = "text/javascript";
        s.src = "//an.yandex.ru/system/context.js";
        s.async = true;
        t.parentNode.insertBefore(s, t);
    })(this, this.document, "yandexContextAsyncCallbacks");
</script>
Example of the invocation code for the second unit
<div id="yandex_rtb_2"></div>
<script type="text/javascript">
    (function(w, d, n, s, t) {
        w[n] = w[n] || [];
        w[n].push(function() {
            Ya.Context.AdvManager.render({
                blockId: "R-A-12345-1",
                renderTo: "yandex_rtb_2",
                async: true,
                pageNumber: 2
            });
        });
        t = d.getElementsByTagName("script")[0];
        s = d.createElement("script");
        s.type = "text/javascript";
        s.src = "//an.yandex.ru/system/context.js";
        s.async = true;
        t.parentNode.insertBefore(s, t);
    })(this, this.document, "yandexContextAsyncCallbacks");
</script>

Container dimensions for displaying the adaptive unit

The adaptive unit adapts to the dimensions of the container and is suitable for sites with an adaptive layout. Use stylesheets to specify the container dimensions for sites with an adaptive layout.

  1. Add the tag div CSS classyandex-adaptive in the invocation code that you obtained in the interface.

    Example
    <div id="yandex_direct_R-A-12345-1" class="yandex-adaptive"></div>
  2. Use media-queries to configure the container to accept various dimensions depending on the screen resolution.

    Example
    @media screen and (max-width: 800px) {
        .yandex-adaptive {
            width:200px;
            height:300px;
        }
    }
    
    @media screen and (max-width: 1024px) {
        .yandex-adaptive {
            width:240px;
            height:400px;
        }
    }

    Thus, ads will be displayed in a 200×300 unit on screens that are up to 800 pixels wide, and they be displayed in a 240×400 unit on screens that are between 800 and 1024 pixels wide. If the screen is more than 1024 pixels wide, then the unit will use the dimensions specified in the interface.

Note. If you want the container to always use the same dimensions, specify them in the visual code editor in the interface.