0
votes

I'm trying to setup Google Ecommerce tracking using a third-party booking system for a restaurant. I can only add html or javascript (in this case Analytics code) in a specific place on the thank-you-page after transaction details.

My question is how can i pass data from booking system output on to Google Ecommerce tracking variables?

Output from Booking system:

    <script type="text/javascript">
    var dataLayer = typeof dataLayer === "undefined" ? [] : dataLayer;

        dataLayer.push({
            "email": "[email protected]",
            "idRestaurant": "202072",
            "nbReservation": "",
            "nbPAX": "5",
            "date": "2017-01-13 18:30:00",
            "event": "booking",
            "idCampaignHash": "15271-59f",
            "caBrut": "10",
            "idResa": "75655429",
            "typePromo": "Enkel reservation"
        });
        var mediaLayer = [].concat(dataLayer);
        for(var i=0;i<mediaLayer.length;i++){
            if(mediaLayer[i]['event']&&mediaLayer[i]['event'].match(/^gtm\./) ){
                mediaLayer.splice(i, 1);
            }
        }
                        mediaLayer.push({
                "email": "[email protected]",
                "event": "nouveauLead"
            });
            dataLayer.push({
                "aboTypeLabel": "Offers",
                "event": "inscritNewsletter"
            });
            dataLayer.push({
                "aboTypeLabel": "Info",
                "event": "inscritNewsletter"
            });



                        dataLayer.push({
        'typeModule': "MRG"
    });

    var pageCategory = 'Thank_you_normal';
    dataLayer.push({
        'pageCategory': pageCategory
    });
</script>
<script type="text/javascript">
    var dataLayer = typeof dataLayer === "undefined" ? [] : dataLayer;
    dataLayer.push({
        'idRestaurant': "12345",
        "nameRestaurant": "Test Restaurant",
        "nameProvider": "",
        "idCampaignHash": "15271-59f"
    });
    dataLayer.push({
        'typeModule': "MRG"
    });
</script>

My Javascript (Analytics Tracking code) after the code above:

<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-11111-1', 'auto');
  ga('send', 'pageview');
  ga('require', 'ecommerce');

  ga('ecommerce:addTransaction', {
  'id': '[idResa]',        // Booking id
  'quantity': '[nbPAX]',   // Number of people.

typePromo
});
ga('ecommerce:addItem', {
  'id': '[idResa]',         // Booking id
  'name': '[typePromo]',    // Type of menu
});
ga('ecommerce:send');
</script>
2

2 Answers

0
votes

The output of the booking system is actually in the Google Tag Manager format. This requires some setup on your side.

See https://www.google.com/tagmanager/ for some more info. You can also combine it with analytics from there.

After this you can add the Tag Manager script to your page, and your tags should be pushed to the Tag Manager.

0
votes

It looks like dataLayer is a global variable so you can easily access the data stored in it: take the first element dataLayer[0] or iterate through elements and find what you need (by checking if nbPAX field exists or etc)

UPDATED

Here is a few examples:

1 to pass individual parameters from first item of dataLayer array

ga('ecommerce:addTransaction', { quantity : dataLayer[0].nbPAX, id : dataLayer[0].idResa })

ga('ecommerce:addItem', { id: dataLayer[0].idResa, name : dataLayer[0].typePromo });

ga('ecommerce:send');

2 Or if you not sure that first item contains necessary information you can filter items and send info to google analytics for each of it for example

//i.e. get all items with parameter idResa
let items = dataLayer.filter(function(item){ return item && item.idResa })

items.forEach(function(item){ 

    ga('ecommerce:addTransaction', { quantity : item.nbPAX, id : item.idResa })
    ga('ecommerce:addItem', { id: item.idResa, name : item.typePromo });
    ga('ecommerce:send');

})