Google Analytics can be great if you want to know how your application is being used. Often with single-page applications though, it’s trickier to get proper screen view analytics and hook into events directly due to the odd API of the official Google Analytics package. However, there is a vue-ua package available that automates much of the complication for you and allows you to integrate Google Analytics into your Vue.js app with ease.
vue-ua can be installed via. Yarn or NPM:
# Yarn
$ yarn add vue-ua -D
# NPM
$ npm install vue-ua --save-dev
Initialize vue-ua in your app by importing the plugin and enabling it with the required configuration parameters. Note that vue-ua only supports app tracking, not web tracking.
If you’re using vue-router, you can add it to the vue-ua config in order to automatically track route views as screen views in GA.
In your components, you can track events, screen views, and exceptions with ease. You also can use the same methods globally anywhere in your app by importing Vue and accessing Vue.analytics.
Tracking Events:
Tracking Screen Views:
Tracking Exceptions:
Change the Language:
You now have all the tools you need to collect data on everything your users do in your Vue.js SPA. Enjoy the unlimited power!
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!