There are a lot of similarities between the data layer and Google Publisher Tag key-values. They are both ways of passing structured (and often dynamic) data from your website to an external program. Both of them consist of a list of key-value pairs.
Your data layer might even contain exactly the same data values as your Google Publisher Tag. For example, here on Technically Product, I include the post ID in both the data layer and in the Google Publisher Tag. If the data layer has the following:
1 2 3 | 'postid': '1363', |
Then the Google Publisher Tag will have the equivalent:
1 2 3 | googletag.pubads().setTargeting('postid', '1363'); |
However while your data layer is most likely to be for the benefit of your tag management system (eg Google Tag Manager), your Google Publisher Tag key-values will be for Google Ad Manager.
In GTM, the data can be used as part of a trigger or a tag; I may want to trigger a tag on a particular post ID, for example. In Google Ad Manager (still often called Google DoubleClick for Publishers, or DFP), it is used for custom ad targeting; I may want to serve an ad on a particular post ID. To summarise:
Structure | Used by | For use with |
---|---|---|
Data layer | Google Tag Manager | Triggers and tags |
Google Publisher Tag key-values | Google Ad Manager | Ad targeting |
I've never quite understood why Google Ad Manager cannot just make use of the data layer. Because of this limitation, you may end up sending the same data to two different Google tools in two different but similar ways. Going back to my postid example, this is how I send the postid to the data layer using php:
1 2 3 | 'postid': '<?php if(is_single()) { global $post; echo $post->ID; } ?>', |
And the equivalent for the data layer:
1 2 3 | googletag.pubads().setTargeting('postid', '<?php global $post; echo $post->ID; ?>'); |
This is obviously a simple example - but if I were ever to change how the data layer value was generated, I would need to remember to make the same change for the Google Publisher Tag too. Wouldn't it be nice if I could just pass my data layer values into my Google Publisher Tag? Then, even if I updated my data layer, I wouldn't need to touch my Google Publisher Tag code.
So I was very pleased to discover that websites built with Abacus' Webvision Cloud platform do exactly that. And Abacus has made the code publicly available as a Gist on Github.
In brief, they use a small script like this before the Google Publisher Tag:
1 2 3 4 5 6 7 8 9 | <script> function getDataLayerVal(keyVal) { for (var i = 0, iL = window.dataLayer.length; i < iL; i++) { if (dataLayer[i][keyVal] !== undefined) { return dataLayer[i][keyVal]; } } } </script> |
And then make use of that function within the Google Publisher Tag as follows:
1 2 3 | googletag.pubads().setTargeting('postid', getDataLayerVal('postid')); |
That passes the data layer value (in this case 'postid') into the Google Publisher Tag. And it removes the need to use php within the Google Publisher Tag code.
Thanks, Abacus - great work!