首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在Nuxt组合Api项目中正确使用Google标记广告?

如何在Nuxt组合Api项目中正确使用Google标记广告?
EN

Stack Overflow用户
提问于 2022-09-29 06:32:25
回答 1查看 119关注 0票数 1

我使用Compositon和ı实现了GPT广告服务。当我第一次加载页面时,它工作得很好,但是如果我在页面之间切换,我的广告区域就会消失,我会得到错误消息。

ads.js

代码语言:javascript
运行
复制
export const adIntegration = () => {
  const adScript = document.createElement('script');
  adScript.type = 'text/javascript';
  adScript.src = 'https://securepubads.g.doubleclick.net/tag/js/gpt.js';
  document.head.appendChild(adScript);
 
};
  
export const mainPageAd = ({ path, size, id }) => {
 
  adIntegration();

  window.googletag = window.googletag || { cmd: [] };

  googletag.cmd.push(() => {
    googletag.defineSlot(path, size, id).addService(googletag.pubads());
    googletag.pubads().enableSingleRequest();
    googletag.enableServices();
    googletag.display(id);
  }); 

};

index.vue

代码语言:javascript
运行
复制
<template lang="pug">
.home-page 
    .advertisement( :id="adSlot.id" style='min-width: 955px; min-height: 240px')
     
<script>
import { mainPageAd } from '~/common/utils/ads';

setup(){ 

 const adSlots = ref({
      path: '/21737763597/adunit-1',
      size: [320, 100],
      id: 'div-gpt-ad-1559997122392-0',
    });

 onMounted(() => {
  mainPageAd({ path: adSlot.value.path, size: adSlot.value.size, id: adSlot.value.id });
    
}

return{adSlots}

我的错误

优先:错误在googletag.defineSlot:无法创建槽/1347001/主-masthead。Div元素"div-gpt-ad-1559997122392-0“已经与另一个插槽相关联: /1347001/main-masthead。

第二种: pubads_impl_2022092701.js?cb=31069995:18异常在排队的GPT命令TypeError:无法读取属性为null (读取'addService')

EN

回答 1

Stack Overflow用户

发布于 2022-09-29 13:26:20

代码语言:javascript
运行
复制
export const adIntegration = () => {
  const adScript = document.createElement('script');

  adScript.type = 'text/javascript';
  adScript.src = 'https://securepubads.g.doubleclick.net/tag/js/gpt.js';
  document.head.appendChild(adScript);
};

export const mainPageAd = ({ path, size, id }) => {
  adIntegration();

  if (window.googletag) window.googletag.pubads().refresh();

  window.googletag = window.googletag || { cmd: [] };

  googletag.cmd.push(() => {
    googletag.defineSlot(path, size, id).addService(googletag.pubads());
    googletag.pubads().enableSingleRequest();
    googletag.enableServices();
    googletag.display(id);
  });
};
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73891196

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档