大侠
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

407 lines
12 KiB

import { director, Director, sys } from "cc";
import { eventNames } from "process";
import { GEData, AnyEventData } from "./GeDifine";
import { PREVIEW } from "cc/env";
import PlatformManager from "../Platform/PlatformManager";
import LocalStorage from "../Mgr_LocalData/LocalStorage";
import { ILevel } from "../Mgr_CSV/CsvEnum";
import CsvMgr from "../Mgr_CSV/CsvMgr";
const config = {
appId: "YOUR_APPID", // 项目 APP ID
serverUrl: "YOUR_SERVER_URL", // 上报地址
autoTrack: {
appLaunch: true, // 自动采集 ta_mg_launch
appShow: true, // 自动采集 ta_mg_show
appHide: true // 自动采集 ta_mg_hide
}
};
interface waitSendData {
name: GEData.GEeventName,
data: AnyEventData
}
export class TDAnalyticsMgr {
private static _ins: TDAnalyticsMgr;
public static get ins() {
if (!this._ins) {
this._ins = new TDAnalyticsMgr();
}
return this._ins;
}
isinit: boolean = false;
isinitComplete:boolean = false;
waitList:waitSendData[]=[];
init(openid: string,weChatAdId:number,version:string) {
if(PREVIEW){return};
if (this.isinit) return;
director.on(Director.EVENT_AFTER_DRAW,this.loopSendEvent,this);
this.isinit = true;
const ConfigData = PlatformManager.instance.platData;
if(!ConfigData.appId || !config.appId.length){
ConsoleEx.Error("数数打点APPID错误")
return;
}
if (!ConfigData.serverUrl || !config.serverUrl.length) {
ConsoleEx.Error("数数打点服务器Url错误")
return;
}
config.appId = ConfigData.TAppID;
config.serverUrl = ConfigData.serverUrl;
//初始化
TDAnalytics.init(config);
// 用户的登录唯一标识,此数据对应上报数据里的#account_id,此时#account_id的值为TA
TDAnalytics.login(openid);
this.setGameName(ConfigData.gameName);
this.userOpenId(openid);
this.lastOpenGame()
this.firstOpenGame()
this.allOpenGameCount()
this.registerPlatform()
this.versionSet(version);
this.weChatAdId(weChatAdId);
ConsoleEx.Log("数数打点数据初始化成功!!!!!!")
// //设置公共事件属性
// var superProperties = {
// channel: "ta", //字符串
// age: 1,//数字
// isSuccess: true,//布尔
// birthday: new Date(),//对象
// object: { key: "value" },//对象
// object_arr: [{ key: "value" }],//对象组
// arr: ["value"]//数组
// };
// TDAnalytics.setSuperProperties(superProperties);
//发送事件
this.isinitComplete = true
}
// 打点操作-用户openid
public setGameName(name:string) {
if (!this.isinit) { return; }
console.log('打点操作-游戏名称', name)
//发送事件
TDAnalytics.userSetOnce({
eventName: "game_name",
properties: { game_name: name }
});
}
// 设置版本属性
public versionSet(version:string) {
if (!this.isinit) { return; }
TDAnalytics.userSet({
properties: {
version: version
}
});
}
// 设置用户属性
public userSet(userName:string) {
if (!this.isinit) { return; }
TDAnalytics.userSet({
properties: {
username: userName
}
});
}
// 设置用户ab属性
public userABSet(abName:string) {
if (!this.isinit) { return; }
console.log('打点操作-用户abtest', abName)
TDAnalytics.userSet({
properties: {
ab: abName
}
});
}
// 设置通关次数属性
public userPassTimeSet(passtime:number) {
if (!this.isinit) { return; }
console.log('打点操作-关次数', passtime)
TDAnalytics.userSet({
properties: {
finish_time: passtime
}
});
}
// 设置试炼挑战属性
public userTrialSet(trialtype:number, rank:number) {
if (!this.isinit) { return; }
console.log('试炼挑战:', trialtype, " || 排行:", rank);
switch(trialtype){
case 0:
TDAnalytics.userSet({
properties: {
ranklist_sword: rank
}
});
break;
case 1:
TDAnalytics.userSet({
properties: {
ranklist_knife: rank
}
});
break;
case 2:
TDAnalytics.userSet({
properties: {
ranklist_stick: rank
}
});
break;
}
}
// 打点操作-用户openid
public userOpenId(openId: any) {
if (!this.isinit) { return; }
console.log('打点操作-用户openid', openId)
//发送事件
TDAnalytics.userSetOnce({
eventName: "open_id",
properties: { open_id: openId }
});
}
// 打点操作-注册平台
public registerPlatform() {
if (!this.isinit) { return; }
console.log('打点操作-注册平台')
//发送事件
TDAnalytics.userSetOnce({
eventName: "platform",
properties: { platform: sys.platform }
});
}
// 打点操作-最后一次打开游戏
public lastOpenGame() {
if (!this.isinit) { return; }
const now = new Date();
const year = now.getFullYear();
const month = now.getMonth() + 1;
const day = now.getDate();
const hours = now.getHours();
const minutes = now.getMinutes();
const seconds = now.getSeconds();
const padZero = (num: number): string => num.toString().padStart(2, '0');
const dateTimeStr = `${year}-${padZero(month)}-${padZero(day)} ${padZero(hours)}:${padZero(minutes)}:${padZero(seconds)}`;
console.log('最后一次打开游戏')
//发送事件
TDAnalytics.userSet({
eventName: "last_open_time",
properties: { last_open_time: dateTimeStr }
});
}
public firstOpenGame() {
if (!this.isinit) { return; }
console.warn('检查首次打开游戏', localStorage.getItem('firstOpenGameDate'));
const now = new Date();
const padZero = (num: number) => num.toString().padStart(2, '0');
// 生成日期key(YYYYMMDD)
const todayKey = [
now.getFullYear(),
padZero(now.getMonth() + 1),
padZero(now.getDate())
].join('');
console.info('firstOpenGame todayKey = ', todayKey);
// 生成完整时间字符串
const fullDateTime = [
`${now.getFullYear()}-${padZero(now.getMonth() + 1)}-${padZero(now.getDate())}`,
`${padZero(now.getHours())}:${padZero(now.getMinutes())}:${padZero(now.getSeconds())}`
].join(' ');
const savedDate = localStorage.getItem('firstOpenGameDate');
this.TrySendtrack(GEData.GEeventName.login_server, {is_first_time: !savedDate});
console.info('firstOpenGame savedDate = ', savedDate);
if (!savedDate) {
// 完全没有记录过首次打开
console.warn('首次记录打开游戏时间:', fullDateTime);
localStorage.setItem('firstOpenGameDate', todayKey);
console.log('打点操作-首次打开游戏');
TDAnalytics.userSetOnce({
eventName: "first_open_time",
properties: { first_open_time: fullDateTime }
});
} else if (savedDate !== todayKey) {
// 有记录但不是今天
console.warn('检测到新的一天,更新打开时间:', fullDateTime);
localStorage.setItem('firstOpenGameDate', todayKey);
} else {
// 今天已经记录过
console.warn('今天已经记录过首次打开时间');
}
}
// 累计打开游戏次数
public allOpenGameCount() {
if (!this.isinit) { return; }
TDAnalytics.userAdd({
eventName: "total_open_cnt",
properties: { total_open_cnt: 1 }
});
}
public addADcnt() {
if (!this.isinit) { return; }
LocalStorage.ad_cnt++;
console.log('addADcnt------>', LocalStorage.ad_cnt)
}
// 打点操作-看广告总次数
public allAdCount() {
if (!this.isinit) { return; }
//发送事件
TDAnalytics.userAdd({
eventName: "total_ad_cnt",
properties: { total_ad_cnt: 1 }
});
}
// 记录首次看广告时间
public recordAdTime() {
if (!this.isinit) { return; }
const now = new Date();
const padZero = (num: number) => num.toString().padStart(2, '0');
const todayKey = [
now.getFullYear(),
padZero(now.getMonth() + 1),
padZero(now.getDate())
].join('');
// 生成完整时间字符串
const fullDateTime = [
`${now.getFullYear()}-${padZero(now.getMonth() + 1)}-${padZero(now.getDate())}`,
`${padZero(now.getHours())}:${padZero(now.getMinutes())}:${padZero(now.getSeconds())}`
].join(' ');
if (localStorage.getItem('firstLookAd') !== todayKey) {
console.warn('首次记录看广告时间:', fullDateTime);
localStorage.setItem('firstLookAd', todayKey);
this.firstAdTime(fullDateTime);
return
}
}
// 打点操作-首次看广告时间
public firstAdTime(time: string) {
if (!this.isinit) { return; }
console.log('打点操作-首次看广告时间')
//发送事件
TDAnalytics.userSetOnce({
eventName: "first_ad_time",
properties: { first_ad_time: time }
});
}
// 打点操作-微信广告id
public weChatAdId(adId: number) {
console.log('微信广告id')
if (adId) {
//发送事件
TDAnalytics.userSetOnce({
eventName: "wx_aid",
properties: { wx_aid: adId }
});
}
}
// 打点操作-projectid
public projectId(projectid: number | string) {
if (!this.isinit) { return; }
console.log('项目id', projectid)
//发送事件
// @ts-ignore
TDAnalytics.userSet({
eventName: "projectid",
properties: { projectid: projectid }
});
// }
}
// 打点操作-promotionid
public promotionid(promotionid: number | string) {
if (!this.isinit) { return; }
console.log('营销id', promotionid)
//发送事件
// @ts-ignore
TDAnalytics.userSet({
eventName: "promotionid",
properties: { promotionid: promotionid }
});
// }
}
// 打点操作-aid
public aid(aid: number | string) {
if (!this.isinit) { return; }
console.log('aid', aid)
//发送事件
// @ts-ignore
TDAnalytics.userSet({
eventName: "aid",
properties: { aid: aid }
});
// }
}
// 打点操作-cid
public cid(cid: number | string) {
if (!this.isinit) { return; }
console.log('cid', cid)
//发送事件
// @ts-ignore
TDAnalytics.userSet({
eventName: "cid",
properties: { cid: cid }
});
}
loopSendEvent(){
if (this.isinit && this.isinitComplete && this.waitList.length > 0) {
let eData = this.waitList.shift();
this.sendtrack(eData.name, eData.data);
}
}
/**上报自定义事件
* @param name 事件名称
* @param data 事件参数
*/
TrySendtrack(name: GEData.GEeventName, data: AnyEventData) {
if (!this.isinit || !this.isinitComplete || this.waitList.length > 0) {
this.waitList.push({ name: name, data: data });
return;
}
this.sendtrack(name,data);
}
/**上报自定义事件
* @param name 事件名称
* @param data 事件参数
*/
private sendtrack(name: GEData.GEeventName, data: AnyEventData) {
TDAnalytics.track({
eventName:name,
properties:data,
});
}
}