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

Commit

Permalink
feat(ecommerce): add ecommerce and ecommerce enhanced features
Browse files Browse the repository at this point in the history
  • Loading branch information
MatteoGabriele committed Nov 23, 2017
1 parent b82dc47 commit 9d245d9
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/collectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,16 @@ export const setters = function () {
}

export const requires = function () {
const ecommerce = ['ec', 'ecommerce']

config.require.forEach(value => {
if (ecommerce.indexOf(value) !== -1 || ecommerce.indexOf(value.name) !== -1) {
throw new Error(
'[vue-analytics] The ecommerce features are built-in in the plugin. \n' +
'Follow the ecommerce instructions available in the documentation.'
)
}

if (typeof value !== 'string' && typeof value !== 'object') {
throw new Error(
'[vue-analytics] Wrong configuration in the plugin options. \n' +
Expand Down
6 changes: 6 additions & 0 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ const defaultConfig = {
set: [],
require: [],

ecommerce: {
enabled: false,
options: null,
enhanced: false
},

autoTracking: {
exception: false,
page: true,
Expand Down
12 changes: 12 additions & 0 deletions src/create-trackers.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ export default function createTrackers () {

config.beforeFirstHit()

const { ecommerce } = config

if (ecommerce.enabled) {
const plugin = ecommerce.enhanced ? 'ec' : 'ecommerce'

if (ecommerce.options) {
query('require', plugin, ecommerce.options)
} else {
query('require', plugin)
}
}

if (config.linkers.length > 0) {
query('require', 'linker')
query('linker:autoLink', config.linkers)
Expand Down
4 changes: 3 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import set from 'lib/set'
import social from 'lib/social'
import time from 'lib/time'
import untracked from 'lib/untracked'
import ecommerce from 'lib/ecommerce'

export default function install (Vue, options = {}) {
Vue.prototype.$ga = Vue.$ga = {
Expand All @@ -21,7 +22,8 @@ export default function install (Vue, options = {}) {
set,
social,
time,
untracked
untracked,
ecommerce
}

update(options)
Expand Down
25 changes: 25 additions & 0 deletions src/lib/ecommerce.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import query from 'lib/query'
import config from '../config'

const getMethod = function (name) {
return `${config.ecommerce.enhanced ? 'ec' : 'ecommerce'}:${name}`
}

const featuresList = [
'addItem',
'addTransaction',
'addProduct',
'addImpression',
'setAction',
'addPromo',
'send'
]

export default featuresList.reduce((coll, feature) => {
return {
...coll,
[feature]: (...args) => {
query(getMethod(feature), ...args)
}
}
}, {})

0 comments on commit 9d245d9

Please sign in to comment.