1、获取SDK
2、引入SDK
- 把下载的两个js文件放到小游戏的工程目录中,比如libs目录下
- 在需要部码的js文件中引入ptagent模块即可,不需要引入ptagent-core.js
var ptagent = require('./js/libs/ptagent.js');
- var ptagent = require('./js/libs/ptagent.js');
var ptagent = require('./js/libs/ptagent.js');
注意:SDK采集的数据会发送到https://mp.ptengine.cn,读取相关SDK配置信息是从https://appconfigloader.ptengine.cn获取。
所以需要在配置服务器信息中添加以上两个域名信息,如下截图所示:

3、接口说明
- Ptagent初始化接口:初始化接口只需要在小游戏入口文件game.js中调用一次即可。确保写在new Main()之前,Main为小游戏主函数,在获取主函数实例前调用Ptagent初始化接口,如下截图所示:
- ptApp ID: 暂时可由ptmind业务人员提供
- appVersion: 小程序的当前版本号
示例:ptagent.PtInit(“ug26cpustmfx&5759y4hwcn”, “1.2.0”);
/**
* Start PtAgent engine, you should invoke it in onShow() lifecyle function of App,
* make sure our module can be inited everytime, even though your app is actived from background mode.
* @param ptApp ID: String, you can get it as soon as registered an application in our web management backend.
* @param appVersion: String, your mini program version number.
*/
PtInit: function(ptAppKey, appVersion){
_ptagent_core.init(ptAppKey, appVersion);
}
- /**
- * Start PtAgent engine, you should invoke it in onShow() lifecyle function of App,
- * make sure our module can be inited everytime, even though your app is actived from background mode.
- * @param ptApp ID: String, you can get it as soon as registered an application in our web management backend.
- * @param appVersion: String, your mini program version number.
- */
- PtInit: function(ptAppKey, appVersion){
- _ptagent_core.init(ptAppKey, appVersion);
- }
/**
* Start PtAgent engine, you should invoke it in onShow() lifecyle function of App,
* make sure our module can be inited everytime, even though your app is actived from background mode.
* @param ptApp ID: String, you can get it as soon as registered an application in our web management backend.
* @param appVersion: String, your mini program version number.
*/
PtInit: function(ptAppKey, appVersion){
_ptagent_core.init(ptAppKey, appVersion);
}
- 采集自定义事件接口:参数值为非空事件名,事件属性为一个{}类型,用于传递关于当前采集事件的一些基本属性信息,可以为空值
示例:ptagent.PtTrackEvent(“购买”, {“商品名称”:“净化器”,“商品价格”: “2000人民币”,。。。。});
/**
* If you want to collect app events by yourself, you can invoke this API
* @param eventName: String, this value can't be nil or empty
* @param properties: {} or undefined, you can bind some event properties for every event,
* if you don't need it, just pass undefined.
*/
PtTrackEvent: function (eventName, properties){
_ptagent_core.trackEvent(eventName, properties);
}
- /**
- * If you want to collect app events by yourself, you can invoke this API
- * @param eventName: String, this value can't be nil or empty
- * @param properties: {} or undefined, you can bind some event properties for every event,
- * if you don't need it, just pass undefined.
- */
- PtTrackEvent: function (eventName, properties){
- _ptagent_core.trackEvent(eventName, properties);
- }
/**
* If you want to collect app events by yourself, you can invoke this API
* @param eventName: String, this value can't be nil or empty
* @param properties: {} or undefined, you can bind some event properties for every event,
* if you don't need it, just pass undefined.
*/
PtTrackEvent: function (eventName, properties){
_ptagent_core.trackEvent(eventName, properties);
}
- 采集转发事件接口:用于统计小游戏的转发事件信息,该事件名为内置值,事件属性可以自由定义,为一个{}类型,可以为空
示例:ptagent.PtTrackSharedEventForMiniProgram({“标题”:“…”, “路径”:“…”});
/**
* Track the shared event if you need, event name is defined by SDK
* you just need to pass the event properties
* @param properties: {}, the properties that you want to describe the event
*/
PtTrackSharedEventForMiniProgram: function (properties) {
_ptagent_core.trackSharedEventForMiniProgram(properties);
}
- /**
- * Track the shared event if you need, event name is defined by SDK
- * you just need to pass the event properties
- * @param properties: {}, the properties that you want to describe the event
- */
- PtTrackSharedEventForMiniProgram: function (properties) {
- _ptagent_core.trackSharedEventForMiniProgram(properties);
- }
/**
* Track the shared event if you need, event name is defined by SDK
* you just need to pass the event properties
* @param properties: {}, the properties that you want to describe the event
*/
PtTrackSharedEventForMiniProgram: function (properties) {
_ptagent_core.trackSharedEventForMiniProgram(properties);
}
- 事件开始接口,用于标记一个事件的开始时间,只有一个参数,参数为非空事件名,事件的时间由SDK进行统计
示例:ptagent.PtBeginEvent(“页面浏览时长”);
/**
* You can invoke this method to mark the begin time for the defined event.
* @param eventName: String, this value can't be nil or empty
*/
PtBeginEvent: function (eventName){
_ptagent_core.beginEvent(eventName);
}
- /**
- * You can invoke this method to mark the begin time for the defined event.
- * @param eventName: String, this value can't be nil or empty
- */
- PtBeginEvent: function (eventName){
- _ptagent_core.beginEvent(eventName);
- }
/**
* You can invoke this method to mark the begin time for the defined event.
* @param eventName: String, this value can't be nil or empty
*/
PtBeginEvent: function (eventName){
_ptagent_core.beginEvent(eventName);
}
- 事件结束接口,用于标记一个事件的结束时间,
- 第一个参数为非空事件名,需要与PtBeginEvent中的事件名保持一致,如果不一致则SDK不会进行统计
- 第二个参数为事件属性信息,可以为空
示例:ptagent.PtEndEvent(“页面浏览时长”, {“页面名称”:“…”, “页面来源”: “…”});
/**
* Mark the end time for defined event, and send event package to server.
* You can specific the event properties for yourself at this moment.
* @param eventName: String, this value can't be nil or empty,
* and must be same with the value passed in PtBeginEvent()
* @param properties: {} or undefined.
*/
PtEndEvent: function(eventName, properties){
_ptagent_core.endEvent(eventName, properties);}
- /**
- * Mark the end time for defined event, and send event package to server.
- * You can specific the event properties for yourself at this moment.
- * @param eventName: String, this value can't be nil or empty,
- * and must be same with the value passed in PtBeginEvent()
- * @param properties: {} or undefined.
- */
- PtEndEvent: function(eventName, properties){
- _ptagent_core.endEvent(eventName, properties);}
/**
* Mark the end time for defined event, and send event package to server.
* You can specific the event properties for yourself at this moment.
* @param eventName: String, this value can't be nil or empty,
* and must be same with the value passed in PtBeginEvent()
* @param properties: {} or undefined.
*/
PtEndEvent: function(eventName, properties){
_ptagent_core.endEvent(eventName, properties);}
- 用户信息采集接口,用于统计当前小程序登录的用户信息
- userID为当前用户名,wxOpenID, wxUnionID可以从微信API处获取,三者不能同时为空
- userProfiles为一个{}类型,用于传递用户的其他基本信息,比如邮件,地址等,可以为空
示例:ptagent.PtTrackUserInfo(“138100…”, “182823233”, “abdckdkdjfda”, {“邮件”:“…”,”地址”:“…”});
/**
* Track the user info if you need, you can also pass the openid, union id by yourself.
* @param userID: String, the account info of your mini program.
* @param wxOpenID: String, openid that acquired from weixin.
* @param wxUnionID: String, unionid that acquired from weixin.
* @param userProfiles: {} or undefined, the other info of the account, such as, email, address...
*/
PtTrackUserInfo: function(userID, wxOpenID, wxUnionID, userProfiles){
_ptagent_core.trackUserInfo(userID, wxOpenID, wxUnionID, userProfiles);
}
- /**
- * Track the user info if you need, you can also pass the openid, union id by yourself.
- * @param userID: String, the account info of your mini program.
- * @param wxOpenID: String, openid that acquired from weixin.
- * @param wxUnionID: String, unionid that acquired from weixin.
- * @param userProfiles: {} or undefined, the other info of the account, such as, email, address...
- */
- PtTrackUserInfo: function(userID, wxOpenID, wxUnionID, userProfiles){
- _ptagent_core.trackUserInfo(userID, wxOpenID, wxUnionID, userProfiles);
- }
/**
* Track the user info if you need, you can also pass the openid, union id by yourself.
* @param userID: String, the account info of your mini program.
* @param wxOpenID: String, openid that acquired from weixin.
* @param wxUnionID: String, unionid that acquired from weixin.
* @param userProfiles: {} or undefined, the other info of the account, such as, email, address...
*/
PtTrackUserInfo: function(userID, wxOpenID, wxUnionID, userProfiles){
_ptagent_core.trackUserInfo(userID, wxOpenID, wxUnionID, userProfiles);
}
- 错误信息采集接口:用于统计当前业务的错误信息数据,exceptionCode, exceptionInfo完全由用户自己定义
示例:ptagent.PtTrackException(“2001”, “购买请求失败”);
/**
* If you need to track some error info package, just invoke it.
* @param exceptionCode: String, error code that you want to defined.
* @param exceptionInfo: String, error or stack/heap info
*/
PtTrackException: function (exceptionCode, exceptionInfo){
_ptagent_core.trackException(exceptionCode, exceptionInfo);
}
- /**
- * If you need to track some error info package, just invoke it.
- * @param exceptionCode: String, error code that you want to defined.
- * @param exceptionInfo: String, error or stack/heap info
- */
- PtTrackException: function (exceptionCode, exceptionInfo){
- _ptagent_core.trackException(exceptionCode, exceptionInfo);
- }
/**
* If you need to track some error info package, just invoke it.
* @param exceptionCode: String, error code that you want to defined.
* @param exceptionInfo: String, error or stack/heap info
*/
PtTrackException: function (exceptionCode, exceptionInfo){
_ptagent_core.trackException(exceptionCode, exceptionInfo);
}
- 虚拟PV采集接口,对于Page的PV数据, SDK会进行自动采集,用户无需对Page页面进行再次采集,但当用户针对于非Page的区域想要定义为一个PV时可以调用该接口进行采集,参数为自定义的非空页面名称和页面属性信息,页面属性信息为{}类型,可以为空
示例:ptagent.PtTrackPVForCustomView(“…”, {“页面路径”:“…”, “页面来源”: “…”});
/**
* For all the Page containers, we have already collected as "pv" data automatically,
* so you DON't need to invoke this API to collect the Page's "pv" data by youself,
* otherwise, pv data will be collected twice with the different name for one Page.
* But in some cases, you can invoke it to define a value as "pv" based on some bussiness for the other view containers (NOT Page), for example ...
* @param pageViewName: String page name, this value can't be empty and nil
* @param pageProperties: {} or undefined, you can pass some info as the properties for the Page
*/
PtTrackPVForCustomView: function (pageViewName, pageProperties){
_ptagent_core.trackPVForCustomView(pageViewName, pageProperties);
}
- /**
- * For all the Page containers, we have already collected as "pv" data automatically,
- * so you DON't need to invoke this API to collect the Page's "pv" data by youself,
- * otherwise, pv data will be collected twice with the different name for one Page.
- * But in some cases, you can invoke it to define a value as "pv" based on some bussiness for the other view containers (NOT Page), for example ...
- * @param pageViewName: String page name, this value can't be empty and nil
- * @param pageProperties: {} or undefined, you can pass some info as the properties for the Page
- */
- PtTrackPVForCustomView: function (pageViewName, pageProperties){
- _ptagent_core.trackPVForCustomView(pageViewName, pageProperties);
- }
/**
* For all the Page containers, we have already collected as "pv" data automatically,
* so you DON't need to invoke this API to collect the Page's "pv" data by youself,
* otherwise, pv data will be collected twice with the different name for one Page.
* But in some cases, you can invoke it to define a value as "pv" based on some bussiness for the other view containers (NOT Page), for example ...
* @param pageViewName: String page name, this value can't be empty and nil
* @param pageProperties: {} or undefined, you can pass some info as the properties for the Page
*/
PtTrackPVForCustomView: function (pageViewName, pageProperties){
_ptagent_core.trackPVForCustomView(pageViewName, pageProperties);
}
/**
* Return PtAgent SDK version
*/
PtSDKVersion: function(){
return _ptagent_core.getSDKVersion();
}
- /**
- * Return PtAgent SDK version
- */
- PtSDKVersion: function(){
- return _ptagent_core.getSDKVersion();
- }
/**
* Return PtAgent SDK version
*/
PtSDKVersion: function(){
return _ptagent_core.getSDKVersion();
}
/**
* Enable PtAgent SDK log output.
*/
PtEnableLogOutput: function(){
_ptagent_core.enableLogOutput();
}
- /**
- * Enable PtAgent SDK log output.
- */
- PtEnableLogOutput: function(){
- _ptagent_core.enableLogOutput();
- }
/**
* Enable PtAgent SDK log output.
*/
PtEnableLogOutput: function(){
_ptagent_core.enableLogOutput();
}