通过第三方代码访问宿主页的googletag广告

时间:2019-06-21 10:52:10

标签: javascript google-dfp google-ad-manager

我的JS将作为第三方代码通过iframe包含在主页上。加载后,我想提供一个新的div容器(“ myDiv”),并访问首页上的一个预先存在的Google Ad Manager实例(该实例已经为参考“ myDiv”定义了一个广告位),并使用它来调用googletag.display('myDiv')在我在iframe中传递的div中。

<!-- Top Host Page Source -->
<html>
    <head>
        <script async='async' src='https://www.googletagservices.com/tag/js/gpt.js'></script>
        <script>
            var googletag = googletag || {};
            googletag.cmd = googletag.cmd || [];

            googletag.cmd.push(function() {
                googletag.defineSlot('/0123456789/Something', [320, 50], 'someOtherDiv').addService(googletag.pubads());
                googletag.defineSlot('/0123456789/Something/else', [320, 50], 'myDiv').addService(googletag.pubads());
                googletag.pubads().enableSingleRequest();
                googletag.enableServices();
            });
        </script>
    </head>
    <body>
        <h1>My Host Top Page</h1>

        <!-- This displays an existing ad on the top host page -->
        <div id="someOtherDiv" style="width:320px; height:50px;">
            <script>
                googletag.cmd.push(()=> { googletag.display('someOtherDiv'); });
            </script>
        </div>

        <!-- 
            Normally if the host page were displaying the 'myDiv' ad here, we'd uncomment the block below,
            but we want to run this functionality from inside the iframe's mycode.html below.
        -->
        <!-- 
        <div id="myDivId" style="width:320px; height:50px;">
            <script>
                googletag.cmd.push(()=> { googletag.display('myDiv'); });
            </script>
        </div>
        -->

        <hr>

        <!-- My code arrives in here and loads mycode.html -->
        <iframe style="width:320px; height:50px;" src="//example.org/mycode.html"></iframe>

    </body>
</html>
<!-- mycode.html Source -->
<html>
    <body>
        <div id="myDiv">
            <script>
                var myGoogletag = top.googletag;

                // Calling top.googletag.pubads().getSlots() here seems to see the slot set up with the 'myDiv' reference.
                // ...But the line below does not do anything.
                myGoogletag.cmd.push(function() { myGoogletag.display("myDiv"); });
            </script>
        </div>
    </body>
</html>

我希望广告会在iframe中显示,但是什么也没有发生,也没有错误。

0 个答案:

没有答案