UNPKG

wxmp-analytics

Version:

Integrate WeChat Mini Program analytics API with Node. Forked from tumobi/weapp-analysis

161 lines (146 loc) 3.51 kB
const axios = require('axios') const url = require('./urls') class MPAnalytics { constructor (appid, secret) { this.appid = appid this.secret = secret } async getAccessToken () { const config = { url: url.ACCESS_TOKEN, params: { grant_type: 'client_credential', appid: this.appid, secret: this.secret } } try { const res = await axios(config) if (!res.data.access_token) { // j } return res.data.access_token } catch (err) { // } } async getDailyTrends (begin_date, end_date) { const config = { url: url.DAILY_TRENDS, data: { begin_date, end_date } } const result = await this.sendRequest(config) return result } async getDailyVisitTrends (begin_date, end_date) { const config = { url: url.DAILY_VISIT_TRENDS, data: { begin_date, end_date } } const result = await this.sendRequest(config) return result } async getWeeklyVisitTrends (begin_date, end_date) { const config = { url: url.WEEKLY_VISIT_TRENDS, data: { begin_date, end_date } } const result = await this.sendRequest(config) return result } async getMonthlyVisitTrends (begin_date, end_date) { const config = { url: url.MONTHLY_VISIT_TRENDS, data: { begin_date, end_date } } const result = await this.sendRequest(config) return result } async getVisitDistribution (begin_date, end_date) { const config = { url: url.VISIT_DISTRIBUTION, data: { begin_date, end_date } } const result = await this.sendRequest(config) return result } async getDailyRetainInfo (begin_date, end_date) { const config = { url: url.DAILY_RETAIN_INFO, data: { begin_date, end_date } } const result = await this.sendRequest(config) return result } async getWeeklyRetainInfo (begin_date, end_date) { const config = { url: url.WEEKLY_RETAIN_INFO, data: { begin_date, end_date } } const result = await this.sendRequest(config) return result } async getMonthlyRetainInfo (begin_date, end_date) { const config = { url: url.MONTHLY_RETAIN_INFO, data: { begin_date, end_date } } const result = await this.sendRequest(config) return result } async getPageVisits (begin_date, end_date) { const config = { url: url.PAGE_VISITS, data: { begin_date, end_date } } const result = await this.sendRequest(config) return result } async getUserPortrait (begin_date, end_date) { const config = { url: url.USER_PORTRAIT, data: { begin_date, end_date } } const result = await this.sendRequest(config) return result } async sendRequest (config) { const {data} = config const url = await this.getRequestUrl(config.url) try { const result = await axios({ method: 'POST', url, data }) return result.data } catch (err) { // err } } async getRequestUrl (url) { const token = await this.getAccessToken() return `${url}?access_token=${token}` } } module.exports = MPAnalytics