我正在尝试找到将Google DFP与Turbolink结合使用的解决方案。
我遵循本指南
但我认为与Turbolink的旧版本有关。
我尝试改编事件监听器:从原始版本开始
class @Gpt
constructor: ->
@slots = {}
window.googletag = googletag || {}
window.googletag.cmd = googletag.cmd || []
$(document).on 'page:fetch', => @clearAds()
$(document).on 'page:load', => @evaluate()
@evaluate()
evaluate: ->
for slot in $('.gpt-ad')
$slot = $(slot)
cachedSlot = @slots[$slot.data('gpt-div-id')]
if cachedSlot? then @refreshSlot(cachedSlot) else @defineSlot($slot)
defineSlot: ($slot) ->
divId = $slot.data('gpt-div-id')
path = $slot.data('gpt-path')
dimensions = $slot.data('gpt-dimensions')
googletag.cmd.push =>
slot = googletag.defineSlot(path, dimensions, divId).addService(googletag.pubads())
googletag.enableServices()
googletag.display(divId)
@slots[divId] = slot
refreshSlot: (slot) ->
googletag.cmd.push ->
googletag.pubads().refresh([slot])
clearAds: ->
googletag.cmd.push ->
googletag.pubads().clear()
我将构造函数更改为
$(document).on 'turbolinks:request-start', => @clearAds()
$(document).on 'turbolinks:load', => @evaluate()
但是不起作用:
googletag.defineSlot(path, dimensions, divId).addService(googletag.pubads())
给我一个错误
pubads_impl_246.js:1 Exception in queued GPT command TypeError: Cannot read property 'addService' of null
有人找到了解决方案吗?