代码之家  ›  专栏  ›  技术社区  ›  Maihan Nijat

如何在Angular中使用GoogleDFP?

  •  0
  • Maihan Nijat  · 技术社区  · 7 年前

    我想使用谷歌DFP(谷歌出版商标签),但它不工作。

    googletag

    googletag: any =  window.googletag || {};
    

    将脚本添加到页面:

    addGPTScript() {
    
      // Ad gtp.js script in the page
      const gads = document.createElement('script');
      gads.async = true;
      gads.type = 'text/javascript';
      const useSSL = 'https:' === document.location.protocol;
      gads.src = (useSSL ? 'https:' : 'http:') + '//www.googletagservices.com/tag/js/gpt.js';
      const node = document.getElementsByTagName('script')[0];
      node.parentNode.insertBefore(gads, node);
    
      if (this.googletag.apiReady) {
        console.log(this.googletag)
      }
    
      console.log(this.googletag)
    }
    
    ngOnInit(): void {
      this.addGPTScript();
    }
    

    但是第一个console.log条件从未加载,第二个条件将打印 undefined . 我检查了HTML HEAD标签,发现添加了脚本。

    1 回复  |  直到 7 年前
        1
  •  0
  •   Maihan Nijat    7 年前

    我注意到以下代码不起作用:

      // Ad gtp.js script in the page
      const gads = document.createElement('script');
      gads.async = true;
      gads.type = 'text/javascript';
      const useSSL = 'https:' === document.location.protocol;
      gads.src = (useSSL ? 'https:' : 'http:') + '//www.googletagservices.com/tag/js/gpt.js';
      const node = document.getElementsByTagName('script')[0];
      node.parentNode.insertBefore(gads, node);
    

    <script type="text/javascript" async src="https://www.googletagservices.com/tag/js/gpt.js"></script>