Skip to content

Commit c68a2c7

Browse files
authored
[fix]modify google analytics gtag implementation (#574)
ref. https://matteo-gabriele.gitbook.io/vue-gtag >Since in Vue 3, the setup method doesn't have access to this.$gtag but you can import the method you need like this Co-authored-by: horiuyas <[email protected]>
1 parent 287b27f commit c68a2c7

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

src/web-ui/src/analytics/AnalyticsHandler.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import Amplitude from 'amplitude-js'
1111
import { RepositoryFactory } from '@/repositories/RepositoryFactory'
1212
import optimizelySDK from '@optimizely/optimizely-sdk';
1313
import { Auth } from 'aws-amplify';
14+
import { set, event } from "vue-gtag";
1415

1516
const RecommendationsRepository = RepositoryFactory.get('recommendations')
1617
const ProductsRepository = RepositoryFactory.get('products')
@@ -182,7 +183,7 @@ export const AnalyticsHandler = {
182183
}
183184

184185
if (this.googleAnalyticsEnabled()) {
185-
this.$gtag.set({
186+
set({
186187
"user_id": user.id,
187188
"user_properties": {
188189
"age": user.age,
@@ -206,7 +207,7 @@ export const AnalyticsHandler = {
206207
})
207208

208209
if (this.googleAnalyticsEnabled()) {
209-
this.$gtag.event("sign_up", {
210+
event("sign_up", {
210211
"method": "Web"
211212
});
212213
}
@@ -228,7 +229,7 @@ export const AnalyticsHandler = {
228229
})
229230

230231
if (this.googleAnalyticsEnabled()) {
231-
this.$gtag.event("login", {
232+
event("login", {
232233
"method": "Web"
233234
});
234235
}
@@ -258,7 +259,7 @@ export const AnalyticsHandler = {
258259
}
259260

260261
if (this.googleAnalyticsEnabled()) {
261-
this.$gtag.event("exp_" + experiment.feature, {
262+
event("exp_" + experiment.feature, {
262263
"feature": experiment.feature,
263264
"name": experiment.name,
264265
"variation": experiment.variationIndex
@@ -372,7 +373,7 @@ export const AnalyticsHandler = {
372373
}
373374

374375
if (this.googleAnalyticsEnabled()) {
375-
this.$gtag.event('add_to_cart', {
376+
event('add_to_cart', {
376377
"currency": "USD",
377378
"value": +product.price.toFixed(2),
378379
"items": [
@@ -506,7 +507,7 @@ export const AnalyticsHandler = {
506507

507508

508509
if (this.googleAnalyticsEnabled()) {
509-
this.$gtag.event('remove_from_cart', {
510+
event('remove_from_cart', {
510511
"currency": "USD",
511512
"value": +cartItem.price.toFixed(2),
512513
"items": [
@@ -651,7 +652,7 @@ export const AnalyticsHandler = {
651652
}
652653

653654
if (this.googleAnalyticsEnabled()) {
654-
this.$gtag.event('view_item', {
655+
event('view_item', {
655656
"currency": "USD",
656657
"value": +product.price.toFixed(2),
657658
"items": [
@@ -749,7 +750,7 @@ export const AnalyticsHandler = {
749750
});
750751
}
751752

752-
this.$gtag.event('view_cart', {
753+
event('view_cart', {
753754
"value": +cartTotal.toFixed(2),
754755
"currency": "USD",
755756
"items": gaItems
@@ -836,7 +837,7 @@ export const AnalyticsHandler = {
836837
});
837838
}
838839

839-
this.$gtag.event('begin_checkout', {
840+
event('begin_checkout', {
840841
"value": +cartTotal.toFixed(2),
841842
"currency": "USD",
842843
"items": gaItems
@@ -971,7 +972,7 @@ export const AnalyticsHandler = {
971972
});
972973
}
973974

974-
this.$gtag.event('purchase', {
975+
event('purchase', {
975976
"transaction_id": order.id.toString(),
976977
"value": +order.total.toFixed(2),
977978
"currency": "USD",
@@ -1028,7 +1029,7 @@ export const AnalyticsHandler = {
10281029
}
10291030

10301031
if (this.googleAnalyticsEnabled()) {
1031-
this.$gtag.event('search', {
1032+
event('search', {
10321033
"search_term": query
10331034
});
10341035
}

0 commit comments

Comments
 (0)