@nobilik said in Trying to install Google Analytics. Very confused!:
@gvorster could you please write step-by-step instruction? I can’t understand what I’m doing wrong…
Here you go, created with the newest Qusar version 1.5.3
quasar new ga_test
save this into file ./src/components/gtm.js
import { uid } from 'quasar'
export default {
logEvent (category, action, label, value) {
dataLayer.push({
'event': 'customEvent',
'category': category,
'action': action,
'label': label,
'value': value,
'cid': this.getCid()
});
},
logPage (path) {
// Here you can preprocess the path, if needed
dataLayer.push({
'event': 'customPageView',
'path': path,
'cid': this.getCid()
});
},
getCid () {
// We need an unique identifier for this session
// We store it in a localStorage, but you may use cookies, too
if (!localStorage.cid) {
localStorage.cid = uid();
}
return localStorage.cid;
},
}
Save this into file ./src/boot/gtm.js
import gtm from 'src/components/gtm';
export default ({ router }) => {
router.afterEach((to, from) => {
gtm.logPage(to.path);
})
}
Add this to quasar.conf.js
boot: [
'gtm'
],
I commented this in quasar.conf.js otherwise a lot of syntax errors are shown
/* extendWebpack (cfg) {
cfg.module.rules.push({
enforce: 'pre',
test: /\.(js|vue)$/,
loader: 'eslint-loader',
exclude: /node_modules/,
options: {
formatter: require('eslint').CLIEngine.getFormatter('stylish')
}
})
} */
Add a test to ./src/pages/Index.vue
<template>
<q-page class="flex flex-center">
<img alt="Quasar logo" src="~assets/quasar-logo-full.svg">
</q-page>
</template>
<script>
import gtm from "../components/gtm";
export default {
name: 'PageIndex',
created() {
gtm.logEvent("myCat", "myAction", "myLabel", 0);
}
}
</script>
Add the Google scripts to ./src/index.template.html and change UA-XXXXXXXXX-X
and GTM-XXXXXXX with your own keys
<!DOCTYPE html>
<html>
<head>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-XXXXXXXXX-X"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag() { dataLayer.push(arguments); }
gtag('js', new Date());
gtag('config', 'UA-XXXXXXXXX-X');
</script>
<!-- Google Tag Manager -->
<script>(function (w, d, s, l, i) {
w[l] = w[l] || []; w[l].push({
'gtm.start':
new Date().getTime(), event: 'gtm.js'
}); var f = d.getElementsByTagName(s)[0],
j = d.createElement(s), dl = l != 'dataLayer' ? '&l=' + l : ''; j.async = true; j.src =
'https://www.googletagmanager.com/gtm.js?id=' + i + dl; f.parentNode.insertBefore(j, f);
})(window, document, 'script', 'dataLayer', 'GTM-XXXXXXX');</script>
<!-- End Google Tag Manager -->
<title><%= htmlWebpackPlugin.options.productName %></title>
<meta charset="utf-8">
<meta name="description" content="<%= htmlWebpackPlugin.options.productDescription %>">
<meta name="format-detection" content="telephone=no">
<meta name="msapplication-tap-highlight" content="no">
<meta name="viewport"
content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width<% if (htmlWebpackPlugin.options.ctx.mode.cordova || htmlWebpackPlugin.options.ctx.mode.capacitor) { %>, viewport-fit=cover<% } %>">
<link rel="icon" type="image/png" href="statics/app-logo-128x128.png">
<link rel="icon" type="image/png" sizes="16x16" href="statics/icons/favicon-16x16.png">
<link rel="icon" type="image/png" sizes="32x32" href="statics/icons/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="96x96" href="statics/icons/favicon-96x96.png">
<link rel="icon" type="image/ico" href="statics/icons/favicon.ico">
</head>
<body>
<!-- DO NOT touch the following DIV -->
<div id="q-app"></div>
</body>
</html>
quasar dev
When loading the app this is what I see in Google Analytics Dashboard real-time view.


