Skip to content

Instantly share code, notes, and snippets.

@supreez
Last active June 13, 2024 10:16
Show Gist options
  • Select an option

  • Save supreez/e3c62c1c7f598d6b171a6d273f3d637d to your computer and use it in GitHub Desktop.

Select an option

Save supreez/e3c62c1c7f598d6b171a6d273f3d637d to your computer and use it in GitHub Desktop.
Setupad tag PHP based website ad unit insertion example
/* Insert <head> code in your header.php file or whatever file you have for <head> code output. Make sure to escape string if needed like double quotes in this example. */
<?php
$head_code = "
<script src=\"https://securepubads.g.doubleclick.net/tag/js/gpt.js\" async></script>
<script>
window.stpd = window.stpd || {};
stpd.que = stpd.que || [];
window.googletag = window.googletag || {};
googletag.cmd = googletag.cmd || [];
googletag.cmd.push (function () {
if ( window.innerWidth >= 1024 ) {
googletag.defineSlot('/123456789/example.com_billboard_desktop', [[970,250],[1000,300]], 'example_com_billboard').addService(googletag.pubads());
} else if ( window.innerWidth >= 768 ) {
googletag.defineSlot('/123456789/example.com_billboard_tablet', [[728,90],[468,60]], 'example_com_billboard').addService(googletag.pubads());
} else {
googletag.defineSlot('/123456789/example.com_billboard_mobile', [[300,250],[336,336]], 'example_com_billboard').addService(googletag.pubads());
}
googletag.pubads().disableInitialLoad();
googletag.pubads().enableSingleRequest();
googletag.pubads().collapseEmptyDivs();
googletag.enableServices();
});
</script>
<script async src=\"https://stpd.cloud/saas/1234\"></script>
";
print($head_code);
?>
/* Now the same process for the ad unit itself. Open your template file like single.php/page.php/etc. or page file wherever you wish to insert an ad unit. */
// Your page code here
<?php
$ad_unit = "
<div id=\"example_com_billboard\">
<script>
googletag.cmd.push(function() { googletag.display('example_com_billboard'); });
</script>
</div>
";
print($ad_unit);
?>
// Continuation of your page code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment