Skip to content

Commit d7bb380

Browse files
committed
初始版本--添加md5模块、添加propagate模块、打点优化
1 parent 7af69df commit d7bb380

13 files changed

+728
-91
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ the sdk offer some general api for multiple platforms, implemented in js
66
## support platform
77
cordova、facebook instant game、wechat mini game
88

9+
## TODO
10+
代码里面有 "// TODO" 的地方均需要自行实现

cordova_sdk/cordova_plugin_admob.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ AdMobObj.prototype._listen = function () {
105105
window['document']['addEventListener']('onAdPresent', function (info) {
106106
logManager.LOGD("onAdPresent...", info, info['adType']);
107107
// logManager.LOGD("onAdPresent..." + JSON.stringify(info));
108-
// TODO
108+
109109
switch (info['adType']) {
110110
case 'banner':
111111
break;
@@ -140,7 +140,7 @@ AdMobObj.prototype._listen = function () {
140140
// 关闭
141141
window['document']['addEventListener']('onAdDismiss', function (info) {
142142
logManager.LOGD("onAdDismiss...", info, info['adType']);
143-
// TODO
143+
144144
switch (info['adType']) {
145145
case 'banner':
146146
break;

fb_sdk/fb_sdk.js

+107
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ var logManager = require('../sdk_log');
99
var loginManager = require('../sdk_login');
1010
var config = require('../sdk_config');
1111
var sdkTool = require('../sdk_tool');
12+
var bi = require('../sdk_bi');
1213

1314
var FBSDK = function () {
1415
// 广告模块
@@ -24,6 +25,67 @@ var FBSDK = function () {
2425
this._initRewardVideoAd();
2526
};
2627

28+
FBSDK.prototype.onLaunch = function () {
29+
if (FBSDK.isInited) {
30+
return ;
31+
} else {
32+
FBSDK.isInited = true;
33+
}
34+
35+
//运行场景上下文, POST - A facebook post. THREAD - A messenger thread. GROUP - A facebook group. SOLO - Default context
36+
var contextType = FBInstant.context.getType();
37+
//启动参数元数据,可能为null
38+
var entryPointData = FBInstant.getEntryPointData() || {};
39+
40+
FBInstant.getEntryPointAsync().then((entrypoint) => {
41+
logManager.LOGD('getEntryPointAsync successful ' + entrypoint);
42+
43+
//类似于微信小游戏的进入的场景参数
44+
// game_switch (游戏切换启动) *
45+
// other (本地调试启动) *
46+
// game_composer (pc版messenger中启动)
47+
// games_hub (客户端messenger中启动)
48+
// admin_message (Game plays coming from a custom update within a messenger thread) custom update updateAsync
49+
// bookmark (来自Facebook 游戏列表中的书签界面)
50+
// bot_cta (Game plays that began from a call-to-action attached to a Messenger bot message) *
51+
// bot_menu (Game plays that began from a Messenger bot menu)
52+
// custom_share (Game plays started from custom shares created by the game with the share API)
53+
// facebook_web (Game plays started from the Facebook front page on web)
54+
// feed (Game plays started from a Facebook feed)
55+
// game_cta (Game plays that began from various call-to-action buttons on Messenger and Facebook)
56+
// game_search (Game plays coming from players searching in Messenger and Facebook search)
57+
// game_share (Game plays started from a generic player share of the Instant Game)
58+
// gameroom (Game plays coming from Facebook Gameroom)
59+
// group (Game plays that started from an entry point in a Facebook group)
60+
// home_screen_shortcut (Game plays coming from our Home Screen Shortcuts API (Android Only))
61+
// menu (Game plays coming from a Messenger menu)
62+
// notification (Game plays initiated by a player engaging with a Facebook notification)
63+
// shareable_link (Game plays that began from a deep link to the game (such as our Instant Game “m.me” links))
64+
// score_share (Game plays that started from a played-shared score)
65+
// video_call (Game plays coming from the Messenger video chat entry point)
66+
// web_games_hub (Game plays started from the Instant Games hub on web, located here)
67+
68+
var event_param = entryPointData;
69+
if(!event_param.from_type){
70+
event_param.from_type = 'normal';
71+
event_param.from_param = '';
72+
}
73+
event_param.entrypoint = entryPoint;
74+
event_param.fb_id = FBInstant.player.getID().toString();
75+
event_param.contextType = contextType;
76+
77+
config.UserInfo.scene_id = entryPoint;
78+
config.UserInfo.scene_param = event_param;
79+
config.UserInfo.device_id = sdkTool.uuidTo32(FBInstant.player.getID());
80+
81+
bi.innerSendEvent(bi.ENUM.ON_LAUNCH, event_param);
82+
83+
}).catch((err) => {
84+
logManager.LOGD('getEntryPointAsync fail ' + JSON.stringify(err));
85+
86+
});
87+
};
88+
2789
/***** 广告模块 *****/
2890

2991
FBSDK.prototype._initInterstitial = function () {
@@ -109,6 +171,20 @@ FBSDK.prototype.showAdsWithPolicy = function (successCallback, failureCallback)
109171
* extraInfo: 扩展信息
110172
*/
111173
FBSDK.prototype.shareToTimeline = function (title, imageUrl, extraInfo, successCallback, failureCallback) {
174+
// bi打点信息
175+
var extData = {
176+
'from_type': 'share',
177+
'share_type': 'indirect',
178+
'share_uuid' : config.UserInfo.uuid.toString(),
179+
'from_user_id' : config.UserInfo.userId.toString(),
180+
'from_fb_id' : config.UserInfo.playerId.toString(),
181+
'fb_id' : config.UserInfo.playerId.toString(),
182+
'share_point_id' : extraInfo.sharePointId,
183+
'share_scheme_id' : extraInfo.shareSchemeId
184+
};
185+
186+
bi.innerSendEvent(bi.ENUM.FB_SHARE_SYNC, extData);
187+
112188
sdkTool.getBase64Image(imageUrl, (imageBase64) => {
113189
logManager.LOGD('get base64 image result ' + imageBase64);
114190

@@ -136,12 +212,30 @@ FBSDK.prototype.shareToTimeline = function (title, imageUrl, extraInfo, successC
136212
* type: 分享类型,'friend':分享给好友 'group':分享到群
137213
*/
138214
FBSDK.prototype.shareToFriend = function (type, title, imageUrl, extraInfo, successCallback, failureCallback) {
215+
// bi打点信息
216+
var extData = {
217+
'from_type': 'share',
218+
'share_type': type,
219+
'share_uuid' : config.UserInfo.uuid.toString(),
220+
'from_user_id' : config.UserInfo.userId.toString(),
221+
'from_fb_id' : config.UserInfo.playerId.toString(),
222+
'fb_id' : config.UserInfo.playerId.toString(),
223+
'share_point_id' : extraInfo.sharePointId,
224+
'share_scheme_id' : extraInfo.shareSchemeId
225+
};
226+
227+
var eventId = type === 'friend' ? bi.ENUM.FB_SHARE_FRIEND : bi.ENUM.FB_SHARE_GROUP;
228+
bi.innerSendEvent(eventId, extData);
229+
139230
sdkTool.getBase64Image(imageUrl, (imageBase64) => {
140231
FBInstant.context.chooseAsync({
141232
'minSize': type === 'friend' ? 0 : 3
142233
}).then((ret) => {
143234
logManager.LOGD('shareToFriend successful ' + JSON.stringify(ret));
144235

236+
var eventId = type === 'friend' ? bi.ENUM.FB_SHARE_FRIEND_SUCCESS : bi.ENUM.FB_SHARE_GROUP_SUCCESS;
237+
bi.innerSendEvent(eventId, extData);
238+
145239
successCallback && successCallback(extraInfo);
146240

147241
FBInstant.updateAsync({
@@ -167,6 +261,9 @@ FBSDK.prototype.shareToFriend = function (type, title, imageUrl, extraInfo, succ
167261
}).catch((err) => {
168262
logManager.LOGD('shareToFriend error ' + JSON.stringify(err));
169263

264+
var eventId = type === 'friend' ? bi.ENUM.FB_SHARE_FRIEND_FAIL : bi.ENUM.FB_SHARE_GROUP_FAIL;
265+
bi.innerSendEvent(eventId, extData);
266+
170267
failureCallback && failureCallback(extraInfo);
171268
});
172269
});
@@ -175,9 +272,17 @@ FBSDK.prototype.shareToFriend = function (type, title, imageUrl, extraInfo, succ
175272
/***** 登录模块 *****/
176273

177274
FBSDK.prototype.loginBySnsId = function (successCallback, failureCallback) {
275+
bi.innerSendEvent(bi.ENUM.GET_FB_PLAYER_INFO_START, config.UserInfo.scene_param);
276+
178277
FBInstant.player.getSignedPlayerInfoAsync('my_metadata').then((result) => {
179278
logManager.LOGD('FBInstant.getSignedPlayerInfoAsync successful ' + JSON.stringify(result));
180279

280+
bi.innerSendEvent(bi.ENUM.GET_FB_PLAYER_INFO_SUCCESS, config.UserInfo.scene_param);
281+
282+
// 初始化一些配置信息
283+
config.UserInfo.playerId = result.getPlayerID();
284+
config.UserInfo.device_id = sdkTool.uuidTo32(FBInstant.player.getID());
285+
181286
return loginManager.loginBySnsIdNoVerify(result.getPlayerID(), {
182287
'snsId': 'fbinstant:' + result.getPlayerID(),
183288
'name': FBInstant.player.getName(),
@@ -192,6 +297,8 @@ FBSDK.prototype.loginBySnsId = function (successCallback, failureCallback) {
192297
}).catch((err) => {
193298
logManager.LOGD('FBSDK.loginBySnsId error ' + JSON.stringify(err));
194299

300+
bi.innerSendEvent(bi.ENUM.GET_FB_PLAYER_INFO_FAIL, config.UserInfo.scene_param);
301+
195302
failureCallback();
196303
});
197304
};

sdk.js

+44-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ let config = require('./sdk_config');
1010
let logManager = require('./sdk_log');
1111
let bi = require('./sdk_bi');
1212
let tool = require('./sdk_tool');
13+
let propagate = require('./sdk_propagate');
1314

1415
// SDK 实例
1516
let _instance = null;
@@ -66,16 +67,56 @@ class SDK {
6667
Object.defineProperty(this.ENUM, key, {
6768
'writable': false
6869
});
69-
};
70+
}
7071
}
7172

72-
// bi日志上报
73+
/***** 放置和公司sdk相关的api *****/
74+
75+
// bi日志上报,eventId必须大于100000
7376
sendBIEvent (eventId, eventParams) {
74-
bi.sendBIEvent(eventId, eventParams);
77+
bi.sendEvent(eventId, eventParams);
78+
}
79+
80+
// 获取Propagate数据
81+
getPropagateInfo () {
82+
return propagate.getShareConfigInfoAutoWeight();
83+
}
84+
85+
// 根据分享tag从Propagate获取单个分享点配置
86+
getShareInfoByTag (tag) {
87+
let info = this.getPropagateInfo();
88+
let config = info[tag] || null;
89+
90+
return config;
91+
}
92+
93+
// 随机获取一个分享点配置
94+
getShareInfoByRandom () {
95+
let config = null;
96+
let shareKeys = [];
97+
let info = this.getPropagateInfo();
98+
99+
for(let key in info){
100+
shareKeys.push(key);
101+
}
102+
103+
if (shareKeys && shareKeys.length > 0) {
104+
let randomIndex = (Math.floor(Math.random() * 10000)) % shareKeys.length;
105+
let sharePointKey = shareKeys[randomIndex];
106+
let config = info[sharePointKey];
107+
}
108+
109+
return config;
75110
}
76111

77112
/***** 放置接入的各平台相关的api *****/
78113

114+
// 主要给需要的平台获取启动场景的参数,在主场景的onLoad中调用
115+
onLaunch () {
116+
logManager.LOGD('sdk.onLaunch...');
117+
this._sdk.onLaunch && this._sdk.onLaunch();
118+
}
119+
79120
showBanner (position) {
80121
logManager.LOGD("sdk.showBanner...");
81122
this._sdk.showBanner && this._sdk.showBanner(position);

sdk_bi.js

+55-8
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,64 @@
44
* create by zengxx on 2018-11-23
55
*/
66

7-
var config = require('./sdk_config');
7+
let config = require('./sdk_config');
8+
let logManager = require('./sdk_log');
9+
let http = require('./sdk_http');
10+
let tool = require('./sdk_tool');
811

9-
var bi = {
10-
init: function () {
12+
// BI 实例
13+
let _instance = null;
1114

12-
},
15+
class BI {
16+
static get instance () {
17+
if (!_instance) {
18+
_instance = new BI();
19+
}
1320

14-
sendEvent: function (eventId, eventParams) {
21+
return _instance;
22+
}
1523

16-
},
17-
};
24+
constructor () {
25+
// TODO 打点上报信息
1826

19-
module.exports = bi;
27+
this._define();
28+
}
29+
30+
_define () {
31+
this.ENUM = {}; // 给实例使用的宏,只读
32+
33+
// TODO BI事件 sdk内部事件ID,游戏自己的记得id大于100000
34+
35+
// 设置为只读
36+
for (let key in this.ENUM) {
37+
Object.defineProperty(this.ENUM, key, {
38+
'writable': false
39+
});
40+
}
41+
}
42+
43+
innerSendEvent (eventId, eventParams) {
44+
// TODO 组织BI参数
45+
46+
logManager.LOGD('BI打点', "eventid= " + eventId + " 描述 = " + JSON.stringify(eventParams));
47+
48+
// TODO 上报BI打点
49+
}
50+
51+
/*
52+
* sdk.js文件中调用,给游戏逻辑调用的打点接口
53+
*
54+
* eventId: 打点事件(必须大于100000,即从100001开始)
55+
* eventParams: 额外参数(默认包含参数fb_appid、fb_playerid字段,若不需要可自行修改tyfb.StateInfo.hasAppInfo值过滤),只能是一级的json,不允许多级嵌套以及array
56+
*/
57+
sendEvent (eventId, eventParams) {
58+
if (eventId > 100000) {
59+
this.innerSendEvent(eventId, eventParams);
60+
} else {
61+
logManager.LOGD('BI打点格式错误, eventId不大于100000');
62+
}
63+
}
64+
}
65+
66+
module.exports = BI.instance;
2067

sdk_config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* create by zengxx on 2018-11-22
55
*/
66

7-
var config = {};
7+
let config = {};
88

99
// 项目配置的一些基本信息
1010
config.baseInfo = {

0 commit comments

Comments
 (0)