yireo-magento1 / Yireo_GoogleTagManager

Implement Google Tag Manager in Magento 1 (deprecated)
Open Software License 3.0
56 stars 46 forks source link

How add additional event attributes? #96

Closed stagemteam closed 6 years ago

stagemteam commented 6 years ago

I need add custom event attributes to my success page. How can I do this?

Example of what I need:

dataLayer.push({
  // next three events should be show on success page
  "event": "gtm-ee-event",
  "gtm-ee-event-category": "Enhanced Ecommerce",
  "gtm-ee-event-action": "Purchase",
  "gtm-ee-event-non-interaction": "False",

  "ecommerce": {
    "purchase": {
      "actionField": {
        "id": "100000074",
        "affiliation": "Magento Shop",
        "revenue": 1503,
        "tax": 0,
        "shipping": 0,
        "coupon": ""
      },
      "products": [{
        "id": "4",
        "sku": "UNT-0003",
        "name": "Safe Box",
        "price": "1,503.00",
        "priceexcludingtax": "1,503.00",
        "tax": "0.00",
        "taxrate": 0,
        "type": "simple",
        "category": "Boxes | Safe",
        "quantity": "1.0000"
      }]
    }
  }
});
jissereitsma commented 6 years ago

The docs of the extension mention that all of the customizations is done on a theming level, meaning either XML files or PHTML templates. Did you already override one of the PHTML templates to see its contents and investigate what to override where?

stagemteam commented 6 years ago

Yes, I've done that. Here is my code after override. Is it correct?

// app/design/frontend/rwd/instor/template/googletagmanager/data/ecommerce.phtml

$toPush = [];
#if ($event = $this->getEcommerceEvent()) {
#    $toPush['event'] = $event;
#}
$toPush['event'] = 'gtm-ee-event';
$toPush['gtm-ee-event-category'] = 'Enhanced Ecommerce';
$toPush['gtm-ee-event-action'] = 'Purchase';
$toPush['gtm-ee-event-non-interaction'] = 'False';

$toPush['ecommerce'] = $this->getEcommerceData();
?>
dataLayer.push(<?php echo json_encode($toPush); ?>);

One unclear moment for me. Where and how to use setEcommerceEvent in a correct way?

jissereitsma commented 6 years ago

Yes, I would test if this is working. setEcommerceEvent() is not an event that you NEED to use, you CAN use it if you want. Naturally, if you understand how the Magento theming layer is working, it doesn't make sense to add data to a block after getEcommerceEvent() has already fetched data from this. So, the setter is there for developers to use in events, overrides and whatever they want to do before rendering blocks.