Reputation: 177
Below is the eCommerse data pushed into the ether.
I want to extract specific bits of the data to push it to Google Tag Manager's (GTM) dataLayer array. Anything hidden, I have no use for (e.g. Credit Card info and etc.). Please note that all of this code is readily available to the public and does not contain sensitive data.
ATTEMPT A:
<script type="text/javascript">//anything below I'm not going to mess with
var _gaq = _gaq || [];
BLACKBAUD.netcommunity.api.DonationConfirmation.add(function(data){
if(data.TransTotal){
_gaq.push(['_addTrans', data.TransID, '',data.TransTotal, '', '', '', '', '']);
for(var i=0;i<data.Items.length;i++){
_gaq.push(['_addItem', data.Items[i].ID, data.Items[i].SKU, data.Items[i].Name, '', data.Items[i].Price, data.Items[i].Quantity]);
};
_gaq.push(['_trackTrans']);
dataLayer.push({gaqBatch_gaq}); //Except for here. I have added this line.
};
});
</script>
This above was my original attempt. It pushes the set of pushes data (3 segments) as such (refer to image).
Google Tag Manager - Preview Mode - Data Layer Push Results (A)
ATTEMPT B:
I revised my snipper above from,
dataLayer.push({_gaq});
to
var gaqTemp = [].concat.apply([], _gaq); //Merges Arrays. Found this on stackoverflow.
dataLayer.push({'gaqBatch' : gaqTemp}); //Pushes Merged Array to dataLayer as 'gaqBatch'
This results in the following: Google Tag Manager - Preview Mode - Data Layer Push Results (B)
What I really want is this:
{
gtm: {
...
},
...
gaqBatch: [
'giftID' : 6024, //from gaqBatch array index 1
'giftAmount' : $25, //from gaqBatch array index 3
'transID' : aaaa-bbbb-cccc-dddd-eeee-ffff, //from gaqBatch array index 11
'giftType' : Donation, //from gaqBatch array index 12
'giftTotal' : $25 //from gaqBatch array index 15
]
}
If I can avoid pushing all the data from the eCommerce snippet into the GTM dataLayer, I'd prefer that. I.e. store it temporarily in an array and create 5 objects: giftID, giftAmount, transID, giftType, giftTotal.
The purpose of these objects would be to use them as dataLayer variables in GTM to feed metrics in Google Analytics.
Thank you,
Sorry for the length, this is my first post and I didn't want to forget any details.
Upvotes: 3
Views: 735
Reputation: 177
Figured it out...
var _gaq = _gaq || [];
var dataLayer = dataLayer || [];
_gaq.push('_addTrans', '9874', '', '115', '', '', '', '', '','_addItem', '5949', '10000-100000-10000-10000-10000000', 'Donation', '', '115', '1','_trackTrans');
var tmpGiftAmount = _gaq[3];
var tmpGiftID = _gaq[1];
var tmpTransID = _gaq[11];
dataLayer.push({'giftAmount' : tmpGiftAmount, 'giftID' : tmpGiftID, 'transID' : tmpTransID});
Upvotes: 1