E
E
eliasj2021-01-13 19:38:34
Yandex
eliasj, 2021-01-13 19:38:34

How to connect the YAN block to a NuxtJs project?

Hello, I saw similar questions, but still need help in solving.
It is necessary to connect the block with advertising (YAN) in the project on NuxtJs.
To do this, I created a separate component,
at the moment its code looks like this

<template>
  <!-- Yandex.RTB R-A-643225-1 -->
  <div id="yandex_rtb_R-A-643225-1"></div>

</template>

<script>
  export default {
    mounted() {
      (function(w, d, n, s, t) {
        w[n] = w[n] || [];
        w[n].push(function() {
          Ya.Context.AdvManager.render({
            blockId: "R-A-643225-1",
            renderTo: "yandex_rtb_R-A-643225-1",
            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>


However, I get an error
NEvTX4PYg1k.jpg?size=1600x860&quality=96&proxy=1&sign=13879eef81cc1ab3a17f19e082e81a05&type=album
I can't figure out how to properly connect the Yandex script

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
eliasj, 2021-01-14
@eliasj

Helped koment: ReaverJS @ReaverJS
Error most likely in different contexts of "this".
I think you can change this to window and this.document to document.
Final code:

<template>
  <!-- Yandex.RTB R-A-643225-1 -->
  <div id="yandex_rtb_R-A-643225-1"></div>

</template>

<script>
  export default {
    mounted() {
      (function(w, d, n, s, t) {
        w[n] = w[n] || [];
        w[n].push(function() {
          Ya.Context.AdvManager.render({
            blockId: "R-A-643225-1",
            renderTo: "yandex_rtb_R-A-643225-1",
            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);
      })(window, document, "yandexContextAsyncCallbacks");
    }
  }
</script>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question