Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display google ad unit multiple times in a single page?

I want to display same ad unit multiple times in a single page. Only the 1st ad slot is working at 1st time. When I click refresh button, then 2nd slot appears. How can I make it work?

Here is my code:

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Google Advertisement</title>
        <script type='text/javascript'>
            var googletag = googletag || {};
            googletag.cmd = googletag.cmd || [];
            (function() {
                var gads = document.createElement('script');
                gads.async = true;
                gads.type = 'text/javascript';
                var useSSL = 'https:' == document.location.protocol;
                gads.src = (useSSL ? 'https:' : 'http:') +
                        '//www.googletagservices.com/tag/js/gpt.js';
                var node = document.getElementsByTagName('script')[0];
                node.parentNode.insertBefore(gads, node);
            })();
        </script>
        <script type='text/javascript'>
            var slots = {};
            googletag.cmd.push(function() {
                slots['sky1'] = googletag.defineSlot('4757460/Home-Above-Fold-300x250', [300, 250], 'ad-sky1').addService(googletag.pubads());
                slots['sky2'] = googletag.defineSlot('4757460/Home-Above-Fold-300x250', [300, 250], 'ad-sky2').addService(googletag.pubads());
                googletag.pubads().enableSingleRequest();
                googletag.enableServices();
            });

            refreshAds = function(refresh_slots) {
                if (typeof(refresh_slots) == "undefined") {
                    googletag.pubads().refresh();
                } else {
                    googletag.pubads().refresh(refresh_slots);
                }
            };

        </script>
    </head>
    <body>
        <div>
            <button onclick="javascript:refreshAds();
                return false;">Refresh All</button>
            <button onclick="javascript:refreshAds([slots['sky1']]);
                return false;">Refresh Sky 1</button>
            <button onclick="javascript:refreshAds([slots['sky2']]);
                return false;">Refresh Sky 2</button>
        </div>
        <div id='ad-sky1' class="ad">
            <script type='text/javascript'>
            googletag.cmd.push(function() {
                googletag.display('ad-sky1');
            });
            </script>
        </div>
        <div id='ad-sky2' class="ad">
            <script type='text/javascript'>
                googletag.cmd.push(function() {
                    googletag.display('ad-sky2');
                });
            </script>
        </div>
    </body>
</html>

To refresh the ad, I have followed https://support.google.com/dfp_sb/answer/3058726?hl=en&ref_topic=1651546

like image 855
Tahmina Khatoon Avatar asked Oct 21 '22 12:10

Tahmina Khatoon


1 Answers

By google adsense TOS one ad unit could displayed only once per page.

You can just create 3 ad units with the same parameters

like image 162
Paulius Avatar answered Oct 23 '22 05:10

Paulius