adopted-ember-addons / adopted-ember-addons/ember-metrics
Explain how dataLayer is instantiated on the window
- 主要語言
- TypeScript
- 星號
- 367
- 分支
- 158
- PR 合併指標
- 30 天內沒有已合併 PR
描述
I don't have _ANY_ Google Tag Manager experience (as of yesterday ;), and so - maybe it isn't addon specific - but I roughhoused with trying to find how to access the GTM dataLayer and wrote all sorts of messes trying to get to it.
If I hadn't just accidentally logged it, I would have kept going down this hole (which was all wrong)
```
const metricsAdapters = this.get('metrics.options.metricsAdapters');
const googleTagManager = metricsAdapters.find( function(thing) {
return thing.name === "GoogleTagManager";
});
let dataLayer = googleTagManager['dataLayer'][0];
```
I think that a note about how dataLayer is global - _based on the way the inserted snippet instantiates is_, would be helpful for people who might not expect it to work that way in an Ember environment.
What do you think?
Whereas this was pretty slick - but just felt too 'magic'
```
router: service(),
metrics: service(),
alertGtmOfPurchase(order) {
const purchaseInfo = {
'event': 'Purchase',
'transactionId': order.order_number,
'exampleKey': 'exampleValue',
],
};
dataLayer.push(purchaseInfo);
},
actions: {
placeOrder() {
this.set('loading', true);
this.get('order').place()
.then( (response)=> {
this.alertGtmOfPurchase(response.order);
this.get('router').transitionTo('checkout.order-confirmation');
})
.catch( (response)=> { ........
```
貢獻指南
評估
這個 Issue 還沒有評估資料。