Skip to content
This repository was archived by the owner on Jun 12, 2024. It is now read-only.

Commit 9d245d9

Browse files
feat(ecommerce): add ecommerce and ecommerce enhanced features
1 parent b82dc47 commit 9d245d9

File tree

5 files changed

+55
-1
lines changed

5 files changed

+55
-1
lines changed

src/collectors.js

+9
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,16 @@ export const setters = function () {
1616
}
1717

1818
export const requires = function () {
19+
const ecommerce = ['ec', 'ecommerce']
20+
1921
config.require.forEach(value => {
22+
if (ecommerce.indexOf(value) !== -1 || ecommerce.indexOf(value.name) !== -1) {
23+
throw new Error(
24+
'[vue-analytics] The ecommerce features are built-in in the plugin. \n' +
25+
'Follow the ecommerce instructions available in the documentation.'
26+
)
27+
}
28+
2029
if (typeof value !== 'string' && typeof value !== 'object') {
2130
throw new Error(
2231
'[vue-analytics] Wrong configuration in the plugin options. \n' +

src/config.js

+6
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ const defaultConfig = {
1010
set: [],
1111
require: [],
1212

13+
ecommerce: {
14+
enabled: false,
15+
options: null,
16+
enhanced: false
17+
},
18+
1319
autoTracking: {
1420
exception: false,
1521
page: true,

src/create-trackers.js

+12
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,18 @@ export default function createTrackers () {
2121

2222
config.beforeFirstHit()
2323

24+
const { ecommerce } = config
25+
26+
if (ecommerce.enabled) {
27+
const plugin = ecommerce.enhanced ? 'ec' : 'ecommerce'
28+
29+
if (ecommerce.options) {
30+
query('require', plugin, ecommerce.options)
31+
} else {
32+
query('require', plugin)
33+
}
34+
}
35+
2436
if (config.linkers.length > 0) {
2537
query('require', 'linker')
2638
query('linker:autoLink', config.linkers)

src/index.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import set from 'lib/set'
1010
import social from 'lib/social'
1111
import time from 'lib/time'
1212
import untracked from 'lib/untracked'
13+
import ecommerce from 'lib/ecommerce'
1314

1415
export default function install (Vue, options = {}) {
1516
Vue.prototype.$ga = Vue.$ga = {
@@ -21,7 +22,8 @@ export default function install (Vue, options = {}) {
2122
set,
2223
social,
2324
time,
24-
untracked
25+
untracked,
26+
ecommerce
2527
}
2628

2729
update(options)

src/lib/ecommerce.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import query from 'lib/query'
2+
import config from '../config'
3+
4+
const getMethod = function (name) {
5+
return `${config.ecommerce.enhanced ? 'ec' : 'ecommerce'}:${name}`
6+
}
7+
8+
const featuresList = [
9+
'addItem',
10+
'addTransaction',
11+
'addProduct',
12+
'addImpression',
13+
'setAction',
14+
'addPromo',
15+
'send'
16+
]
17+
18+
export default featuresList.reduce((coll, feature) => {
19+
return {
20+
...coll,
21+
[feature]: (...args) => {
22+
query(getMethod(feature), ...args)
23+
}
24+
}
25+
}, {})

0 commit comments

Comments
 (0)