mp-ga-tracker
v1.1.1
Published
GA tracking SDK for WeChat Miniprogram.
Downloads
3
Maintainers
Readme
mp-ga-tracker
GA tracking SDK for WeChat Miniprogram.
Installation
$ npm install mp-ga-tracker --save
Initialization
import GaTracker, { DS_WEB, DS_APP } from 'mp-ga-tracker'
const config = {
server: {
host: '<ga_proxy_server_host_url>',
// ?optional, default -> '/collect'
singleHitEndpoint: '<ga_single_hit_collect_endpoint>',
// ?optional, default -> '/batch'
batchHitEndpoint: '<ga_multiple_hits_collect_endpoint>',
// ?optional, default -> `POST`
method: 'POST',
},
// set the default fallback value to the dh
defaultDh: 'default.document_host_name.com',
// set the params that would be sent for every single hit
generalParams: {
tid: '<ga_tracking_id>',
// ?optional, protocol version, default -> 1
v: 1,
// ?optional, data source, enum value would be either DS_WEB or DS_APP, default -> DS_WEB
ds: DS_WEB,
// required when the ds setting above is DS_APP, otherwise the value won'be used
an: '<application_name>',
// ?optional, application id
aid: '<application_id>',
// ?optional, application version
av: '<application_version>',
},
// ?optional, for the hit console logging, default -> false
logging: false,
}
const gaTracker = new GaTracker(config)
Usage
/* SETTING UP */
// Setting the client ID, which normally would be WeChat user's openId
// Note that the cid should be set up ASAP once the gaTracker has been initialized
// The ga hit would be pushed into the internal queue and wouldn't be sent to the server before the cid has been set
gaTracker.setCid('the_client_id')
// Setting the campaign params
// All of these params are optional and could be set at any time
// Every single hit in the internal queue that haven't been sent out would be affected when the campaign params updates
gaTracker.setCampaignParams({
cn: 'campaign_name',
cs: 'campaign_source',
cm: 'campaign_medium',
ck: 'campaign_keyword',
cc: 'campaign_content',
})
/* TRACKING */
// Tracking screenview, either string or object is ok to be used
gaTracker.trackScreenview('screen_name', ...enhancers)
gaTracker.trackScreenview({
// required
cd: 'sceen_name',
// ?optional, default -> 0
ni: 1,
}, ...enhancers)
// Tracking pageview
gaTracker.trackPageview({
// required, document_title
dt: 'document_title',
// opitonal if the defaultDh has been set up when the gaTracker is initialized
dh: 'document_host_name',
// ?optional, document_path, default -> the MP current page path
dp: 'dopcument_path',
// ?optional, default -> 0
ni: 1,
}, ...enhancers)
// trackPageview supports string argument as well, it would be treated as the dt value
// Note that it won't work if the defaultDh hasn't been setup
gaTracker.trackPageview('document_title', ...enhancers)
// Tracking event
// Besides the event info, extra screenview / pageview info would be attached as well based on the ds (data_source) value:
// when the ds is DS_WEB, the last pageview info would be attached
// when the ds is DS_APP, the last screenview info would be attached
gaTracker.trackEvent({
// required
ec: 'event_category',
// reuqired
ea: 'event_action',
// ?optional
el: 'event_label',
// ?optional
ev: 'event_value',
// ?optional, default -> 0
ni: 1,
}, ...enhancers)
List of enhancement plugins
- mp-ga-enhancer-ecommerce
$ npm install mp-ga-enhancer-ecommerce --save