Skip to content

Instantly share code, notes, and snippets.

@afjlambert
Last active August 29, 2015 14:18
Show Gist options
  • Save afjlambert/cfff96ac55d40984ef2b to your computer and use it in GitHub Desktop.
Save afjlambert/cfff96ac55d40984ef2b to your computer and use it in GitHub Desktop.
Create a new custom tag in your tagmanager and paste this in. Add the html div to your page.
<script>
/*
Replace the 'pub-xxxxxxxxxxxxxxxx' with your own Google Adsense publisher-id.
Update the 'positions' with the data from your Google Adsense account page.
Usage:
<div class="tagmanager_ad" data-name="rectangle"></div>
*/
var publisherId = "pub-xxxxxxxxxxxxxxxx",
positions = [
{ 'data-name': 'rectangle', 'ad-slot-id': 1231231231, 'width': 336, 'height': 280 },
{ 'data-name': 'leaderboard', 'ad-slot-id': 2312312312, 'width': 468, 'height': 60 }
];
// ========== Do not edit below this line ==========
var ads = document.getElementsByClassName('tagmanager_ad'),
found = false;
for (var i = 0; i < ads.length; i++) {
var adDiv = ads[i],
name = adDiv.getAttribute('data-name');
for (var pos in positions) {
var position = positions[pos];
if (name === position['data-name']) {
found = true;
adDiv.innerHTML = '<ins class="adsbygoogle" style="display:inline-block;width:' + position.width+ 'px;height:' + position.height + 'px" data-ad-client="ca-' + publisherId + '" data-ad-slot="' + position['ad-slot-id'] +'"></ins>';
}
}
}
(adsbygoogle = window.adsbygoogle || []).push({});
if (found) {
var script = document.createElement('script');
script.type = 'text/javascript';
script.async = true;
script.src = document.location.protocol + '//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(script, s);
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment