Reputation:
I am trying to build remarketing audience lists within Google Analytics using GTM. I need some help figuring out how to pass custom variables via GTM thru the datalayer and into GA.
So far, I have been able to successfully do this by hard coding some custom code and passing custom variables directly into GA (bypassing GTM). However, when I use this code it skews the GA numbers b/c 2 instances of the same GA that are present via custom code, and existing GTM instance. Which is great since it inflates the numbers favorably ;) Unfortunately, we are currently very dependent GTM, and therefore cannot abandon it.
What we are now trying to do is modify the working code to use with GTM so that we are not inflating our numbers as mentioned. Thus far, we can see this data in the datalayer by inspecting the console in chrome. However, we haven’t been able to push this data to GA.
Here is an example of the code that we used previously which does not rely on GTM and is successfully pushing the custom variables to GA.
<script>var _gaq = _gaq || [];_gaq.push(["_setAccount", "UA-XXXXXX-X"]);_gaq.push(['_setCustomVar', 1, "Category", "testing", 3]);_gaq.push(['_setCustomVar', 2, "content-type", "Brief", 3]);_gaq.push(['_setCustomVar', 3, "publish-date", "Wednesday, July 16, 2014 - 17:42", 3]);_gaq.push(['_setCustomVar', 4, "author", "Greg Bates", 3]);_gaq.push(["_trackPageview"]);(function() {var ga = document.createElement("script");ga.type = "text/javascript";ga.async = true;ga.src = ("https:" == document.location.protocol ? "https://ssl" : "http://www") + ".google-analytics.com/ga.js";var s = document.getElementsByTagName("script")[0];s.parentNode.insertBefore(ga, s);})();</script>
For the current GTM implementation, there is a bit of a hybridization using GTM and hard coding a newer script which I will detail below.
In my standard GA tag for GTM it is setup as follows:
Tag Name: GA Universal
Tag Type: universal analytics
Tracking ID: UA-XXXXXX-X
Enable Display Advertising Features √
Track Type: Page View
Firing Rules: All pages
More Settings>>Custom Dimensions:
Index = 4 Dimension = {{Author}}
Index = 3 Dimension = {{Publish Date}}
Index = 2 Dimension = {{Content Type}}
Index = 1 Dimension = {{Page Category}}
The custom code that is hardcoded into the page manually is as follows:
<script>
if (Drupal.settings.common !== undefined && Drupal.settings.common.pageCategory !== undefined) {
dataLayer = [{
'pageCategory': Drupal.settings.common.pageCategory
}];
var google_tag_params = {
section: Drupal.settings.common.pageCategory,
pagetype: ' ',
membertype: ' '
};
if (Drupal.settings.common.contentType !== undefined) {
dataLayer.push({'contentType': Drupal.settings.common.contentType});
google_tag_params.contenttype = Drupal.settings.common.contentType;
}
if (Drupal.settings.common.publishDate !== undefined) {
dataLayer.push({'publishDate': Drupal.settings.common.publishDate});
google_tag_params.publishdate = Drupal.settings.common.publishDate;
}
if (Drupal.settings.common.author !== undefined) {
dataLayer.push({'author': Drupal.settings.common.author});
google_tag_params.author = Drupal.settings.common.author;
}
}
(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'//www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-XXXXXX');</script> <noscript><iframe src="//www.googletagmanager.com/ns.html?id=GTM-XXXXXX" height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
I‘ll also add that Drupal.settings.common.author, Drupal.settings.common.contentType, Drupal.settings.common.pageCategory and Drupal.settings.common.publishDate are defined before executing the GTM script.
Can anybody provide some helpful insight as to what we are doing incorrect and what can be done to pass these custom variables into Google Analytics?
If it would be helpful to see a live implementation, please follow this link http://api.pw/Uem8ba .
Thanks in advance of your time and help!
Brett
Upvotes: 1
Views: 2668
Reputation: 3078
ok, first of all, since it's Drupal - drop your custom implementation in favor of these two modules:
https://www.drupal.org/project/google_tag
https://www.drupal.org/project/datalayer
Configure GTM container ID, and pass all the data you need via dataLayer - if after enabling all checkboxes in data_layer module you don't have everything you need in data layer, you have to use alter hook from this module, to push your variables there - since your code is ready it should be very easy.
Next, you have to configure GTM to collect your data in GTM and pass it to GA, so:
nobody said using GTM is simple ;)
Upvotes: 3