V
V
Vladislav Chernushevich2017-04-07 11:52:17
Yandex
Vladislav Chernushevich, 2017-04-07 11:52:17

How to tie YAN to reactjs?

Yandex issues such a code for its advertising. How to properly tie it to reactjs?

<!-- Yandex.RTB XXXXX -->
<div id="yandex_rtb_XXXXXX"></div>
<script type="text/javascript">
    (function(w, d, n, s, t) {
        w[n] = w[n] || [];
        w[n].push(function() {
            Ya.Context.AdvManager.render({
                blockId: "XXXXXX",
                renderTo: "yandex_rtb_XXXXXXX",
                horizontalAlign: false,
                async: true
            });
        });
        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>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vlad Feninets, 2017-04-07
@fnnzzz

just paste it into the html, below the div where react is rendered, because react only "diffs" the entry-node you specified.
if you want to add in the react itself, from under the component, then you can do this

appendYaMetrix() {
  const yaDiv = document.createElement('div')
  yaDiv.setAttribute('id', 'yandex_rtb_XXXXXX')
  document.body.appendChild(yaDiv)

  const yaScript = document.createElement('script')
  yaScript.setAttribute('type', 'text/javascript')
  yaScript.innerHTML = `(function(w, d, n, s, t) {
        w[n] = w[n] || [];
        w[n].push(function() {
            Ya.Context.AdvManager.render({
                blockId: "XXXXXX",
                renderTo: "yandex_rtb_XXXXXXX",
                horizontalAlign: false,
                async: true
            });
        });
        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");`

    document.head.appendChild(yaScript)
}

componentDidMount() {
  this.appendYaMetrix()
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question