簡體   English   中英

如何在Angular中使用Google DFP?

[英]How to use Google DFP in Angular?

我想使用Google DFP(Google發布商代碼),但無法正常工作。

我定義了包含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 in條件不會被加載,而第二個條件會打印undefined 我檢查了HTML HEAD標記,發現已添加腳本。

我注意到以下代碼無法正常工作:

  // 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);

我刪除了上面的內容,並添加了HTML Head標簽:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM