Skip to content
This repository was archived by the owner on Mar 21, 2025. It is now read-only.

Commit ef1fa26

Browse files
fix(vuex-middleware): impossible to use ecommerce in vuex
the ecommerce is different from the others and needs a special treatment
1 parent 230faf3 commit ef1fa26

File tree

2 files changed

+51
-4
lines changed

2 files changed

+51
-4
lines changed

docs/vuex.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,11 @@ The way we can construct the track method is just by creating an array and the f
8888
* social
8989
* time
9090
* untracked
91-
* ecommerce
91+
* ecommerce (*)
9292
* commands
9393

94+
**(*) pay attention to the ecommerce API at the bottom of this page, since it has a different structure**
95+
9496
the second parameter will be our data constructed the same way as we were inside a component, for example:
9597

9698
```js
@@ -166,3 +168,24 @@ commit('someAction', {
166168
}
167169
})
168170
```
171+
172+
### Ecommerce and Vuex
173+
174+
Because of its differnet API nature, the ecommerce plugin needs to be called as it in the original Google Analytics documentation.
175+
176+
```js
177+
commit('someAction', {
178+
meta: {
179+
analytics: [
180+
['ecommerce:addItem', {
181+
id: '1234',
182+
name: 'Fluffy Pink Bunnies',
183+
sku: 'DD23444',
184+
category: 'Party Toys',
185+
price: '11.99',
186+
quantity: '1'
187+
}]
188+
]
189+
}
190+
})
191+
```

src/vuex-middleware.js

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,38 @@ export default store => {
1313
}
1414

1515
analytics.forEach(event => {
16-
const type = event.shift()
16+
let method
17+
let type = event.shift()
18+
1719
const props = event
1820

21+
if (type.includes(':')) {
22+
[type, method] = type.split(':')
23+
}
24+
1925
if (!(type in lib)) {
20-
throw new Error(`The type "${type}" doesn't exist.`)
26+
throw new Error(
27+
`[vue-analytics:vuex] The type "${type}" doesn't exist.`
28+
)
29+
}
30+
31+
if (method && !(method in lib[type])) {
32+
throw new Error(
33+
`[vue-analytics:vuex] The type "${type}" has not method "${method}".`
34+
)
35+
}
36+
37+
if (type === 'ecommerce' && !method) {
38+
throw new Error(
39+
`[vue-analytics:vuex] The type "${type}" needs to call a method. Check documentation.`
40+
)
2141
}
2242

23-
lib[type](...props)
43+
if (method) {
44+
lib[type][method](...props)
45+
} else {
46+
lib[type](...props)
47+
}
2448
})
2549
})
2650
}

0 commit comments

Comments
 (0)