Baidu statistics vue Ba use case explanation in vue single page project

Posted by Marc on Sun, 23 Jan 2022 16:42:14 +0100

install

npm install vue-ba --save
    

Get started quickly

Reference directly in the page

    
  1. <script src="./node_modules/vue-uweb/dist/index.js"> <script>

Or load through es6 module

import ba from 'vue-ba'
    

Using the Vue Ba plug-in

Vue.use(ba, 'YOUR_SITEID_HERE')
    

More settings are made by passing the options parameter

Vue.use(uweb,options)
    

main.js settings

    
  1. ...
  2. Vue.config.productionTip = false;
  3. Vue. use(ba, "Baidu Statistics website key");
  4. Vue. use(ba, { siteId: "Baidu Statistics website key" });
  5. new Vue({
  6. ...
  7. })

options

parametermustdefaultexplainremarks
siteIdyesBind the statistics code to accept the API request
debugnofalseIn debug mode, the output of the console will call window_ Parameters passed during HMTPlease do not use it in the production environment to avoid potential safety hazards
srcnohttps://hm.baidu.com/hm.js?SITEIDSpecifies the src attribute of the statistics script tag

2. ba API

2.1 trackEvent

It is used to send event statistics requests when interactive elements such as buttons on the page are triggered.

usage

this.$ba.trackEvent(category,action,opt_label,opt_value)

    

parameter

parametermusttypeexplain
categoryyesstringIndicates who the event happened to
actionyesstringRepresents the behavior of a visitor interacting with an element
opt_labelnostringUsed to describe events in more detail
opt_valuenointIt is used to fill in the scoring value of scoring type events and the duration of loading time type events. If it is filled in other forms, the system will treat it as 0. If it is a floating point decimal, the system will automatically round off and remove the decimal point.

2.2 trackPageview

It is used to send PV statistics requests for a URL. It is applicable to statistics of AJAX, asynchronously loaded pages, friendship links and download links

usage

    
  1. this.$ba.trackPageview(pageURL)

parameter

parametermusttypeexplain
pageURLyesstringCustomize the URL address of the virtual PV page, fill in the relative path starting with the slash '/', and the system will automatically complete the domain name

3. ba directive

Vue Ba provides two instructions: track event and track pageview. Developers can directly use them in html templates to count website data

3.1 track-event

Use the instruction v-track-event to listen for events. Specify the event type through modifiers. Event listening will be automatically added for binding elements. When an event triggers, call the statistics code. If no event is specified, the click event will be listened to by default.

Parameters can be passed through comma separated string or object literal. Please pay attention to the parameter order when passing in string. Refer to trackEvent API.

Use v-track-event.com to count custom events someEvent:custom

usage

    
  1. <button v-track- event.click= "'category, action''"></button> // Count click events
  2. <button v-track- event= "'category, action'"></button> // Statistical click event abbreviation
  3. <input v-track- event.keypress= "'category, action'"> // Count keypress events
  4. <input v-track- event.someEvent:custom= "'category, action'"> // Count someEvent events. someEvent is a custom event
  5. <button v-track- event= "'category, action, opt_label, opt_value'"><button> // Pass parameters as strings
  6. <button v-track- event= "{category:'event', action:'click'}"></button> // Passing parameters as object literals

3.2 track-pageview

The instruction track pageview is used to count the virtual PV. Generally, it can be used in conjunction with v-show or v-if to count the PV of the local dynamic view.

Parameters can be passed through comma separated string or object literal. Please pay attention to the parameter order when passing in string. Please refer to the usage of trackPageview API

    
  1. <div v-show="show" v-track-pageview="'/bar'">bar </div> // Virtual pv that tracks v-show binding elements
  2. <div v-if="show" v-track-pageview="'/foo'">foo </div> // Virtual pv that tracks v-if binding elements
  3. <div v-track-pageview="'/tar'"> </div> // Specify the visited page and source as a string
  4. <div v-track-pageview="{pageURL:'/zoo''}"> </div> // Specify the visited page and source in object literals

Reprint: https://blog.csdn.net/JackieDYH/article/details/118309191