一、阿里云配置
https://sls.console.aliyun.com/lognext/profile
点击“创建Project”,我们现在命名为minigamelog。
然后点击进入minigamelog:
选择“日志存储”,点击“+”,创建“logstore”:
这里,我们命名为test。
之后,点击“仪表盘”,进入仪表盘界面:
点击“+”,创建“仪表盘”
创建完后的“测试”仪表盘,是这样的:
我们需要往仪表盘中加入各种sql语句,来筛选我们需要的数据信息。比如,我现在开发的项目数据是这样:
最后,我们配置日志的字段属性:
至此,阿里云的“日志服务”的配置算是初步完成了,接下来需要编写游戏中上传到“日志服务”的代码,来保存更多的数据,从而筛选更均衡的数据。
二、代码调用
以下是日志分析的代码,文件名为“analytics_al.js”
const version = '0.0.1';
const project = 'minigamelog';
const host = 'cn-shanghai.log.aliyuncs.com';
const logstore = 'test';
class SK {
constructor() {
this.playerId = '';
this.appid = '';
this.fingerprintId = '';
this.eventsArr;
this.isSend;
this.sendCount;
this.interstitialArr;
this.levelConfig;
this.createdAt;
this.loginDays;
this.channel = '';
this.cversion = '';
this.platform;
this.engine;
this.scene = '';
this.brandmodel = '';
this.startTime = Date.now();
}
static aliAjaxGet(url) {
var xhr = new XMLHttpRequest();
let _this = this;
let sendTimeout = setTimeout(function () {
_this.sendEvent();
}, 10000);
xhr.open('GET', url, true);
xhr.onreadystatechange = () => {
if (xhr.readyState == 4) {
if (xhr.status == 200 || xhr.status == 304) {
clearTimeout(sendTimeout);
this.eventsArr.shift();
this.sendEvent();
} else {
if (this.sendCount < 5) {
this.sendCount++;
this.sendEvent();
}
}
}
};
xhr.send();
}
static aliAjaxGetSK(url) {
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.onreadystatechange = () => {
};
xhr.send();
}
static sendEvent() {
if (!this.playerId) {
setTimeout(() => {
this.sendEvent();
}, 1000);
return;
}
if (this.eventsArr.length > 0) {
let eventData = this.eventsArr[0];
let sendData = typeof eventData[1] === 'object' ? JSON.stringify(eventData[1]) : eventData[1];
//云API
let domain = `https://${project}.${host}/logstores/${logstore}/track`;
let url = `${domain}?APIVersion=0.6.0&appid=${this.appid}&playerId=${this.playerId}&fingerprintId=${this.fingerprintId}&event=${eventData[0]}&data=${sendData}&createdAt=${this.createdAt}&loginDays=${this.loginDays}&channel=${this.channel}&version=${version}&cversion=${this.cversion}&platform=${this.platform}&engine=${this.engine}&scene=${this.scene}&brandmodel=${this.brandmodel}`;
this.aliAjaxGet(url);
} else {
this.isSend = false;
}
}
window.sk = SK;
最后,只需要像游戏打点一样在游戏中需要的时候调用sendEvent,即可。