AnalyticsTest.ts 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /**
  2. * 埋点功能测试脚本
  3. * 用于验证埋点管理器的各项功能是否正常工作
  4. */
  5. import { _decorator, Component, Node, Label, Button } from 'cc';
  6. import { Analytics } from './AnalyticsManager';
  7. import { MPLifecycle } from './MPLifecycleManager';
  8. const { ccclass, property } = _decorator;
  9. @ccclass('AnalyticsTest')
  10. export class AnalyticsTest extends Component {
  11. @property(Label)
  12. statusLabel: Label = null;
  13. @property(Button)
  14. testButton: Button = null;
  15. private testResults: string[] = [];
  16. start() {
  17. this.updateStatus('埋点测试准备就绪');
  18. if (this.testButton) {
  19. this.testButton.node.on('click', this.runAllTests, this);
  20. }
  21. }
  22. /**
  23. * 运行所有测试
  24. */
  25. public runAllTests(): void {
  26. this.testResults = [];
  27. this.updateStatus('开始运行埋点测试...');
  28. // 测试基础埋点功能
  29. this.testBasicTracking();
  30. // 测试小程序生命周期事件
  31. this.testMPLifecycleEvents();
  32. // 测试场景加载事件
  33. this.testSceneLoadEvents();
  34. // 测试游戏进度事件
  35. this.testGameProgressEvents();
  36. // 显示测试结果
  37. this.showTestResults();
  38. }
  39. /**
  40. * 测试基础埋点功能
  41. */
  42. private testBasicTracking(): void {
  43. try {
  44. // 测试自定义事件
  45. Analytics.track('test_custom_event', {
  46. test_property: 'test_value',
  47. test_number: 123,
  48. test_boolean: true
  49. });
  50. this.addTestResult('✅ 基础埋点功能测试通过');
  51. } catch (error) {
  52. this.addTestResult('❌ 基础埋点功能测试失败: ' + error.message);
  53. }
  54. }
  55. /**
  56. * 测试小程序生命周期事件
  57. */
  58. private testMPLifecycleEvents(): void {
  59. try {
  60. // 测试启动事件
  61. Analytics.trackMPLaunch({
  62. scene: 1001,
  63. query: '{"test": "value"}',
  64. shareTicket: 'test_ticket'
  65. });
  66. // 测试显示事件
  67. Analytics.trackMPShow({
  68. scene: 1001,
  69. query: '{"test": "value"}'
  70. });
  71. // 测试隐藏事件
  72. Analytics.trackMPHide();
  73. this.addTestResult('✅ 小程序生命周期事件测试通过');
  74. } catch (error) {
  75. this.addTestResult('❌ 小程序生命周期事件测试失败: ' + error.message);
  76. }
  77. }
  78. /**
  79. * 测试场景加载事件
  80. */
  81. private testSceneLoadEvents(): void {
  82. try {
  83. // 测试场景加载开始
  84. Analytics.trackSceneLoadStart('TestScene');
  85. // 模拟加载时间
  86. setTimeout(() => {
  87. // 测试场景加载成功
  88. Analytics.trackSceneLoaded({
  89. scene_name: 'TestScene',
  90. load_time: 1500,
  91. success: true
  92. });
  93. // 测试场景加载失败
  94. Analytics.trackSceneLoaded({
  95. scene_name: 'FailedScene',
  96. load_time: 3000,
  97. success: false,
  98. error_msg: 'Scene not found'
  99. });
  100. // 测试场景卸载
  101. Analytics.trackSceneUnloaded('TestScene');
  102. }, 100);
  103. this.addTestResult('✅ 场景加载事件测试通过');
  104. } catch (error) {
  105. this.addTestResult('❌ 场景加载事件测试失败: ' + error.message);
  106. }
  107. }
  108. /**
  109. * 测试游戏进度事件
  110. */
  111. private testGameProgressEvents(): void {
  112. try {
  113. // 测试完成关卡
  114. Analytics.trackCompleteSection({
  115. level: 5,
  116. score: 8500,
  117. time_spent: 120,
  118. result: 'success'
  119. });
  120. // 测试等级提升
  121. Analytics.trackUpdateLevel(4, 5);
  122. // 测试新手引导
  123. Analytics.trackTutorialStart();
  124. setTimeout(() => {
  125. Analytics.trackTutorialFinish(300);
  126. }, 50);
  127. this.addTestResult('✅ 游戏进度事件测试通过');
  128. } catch (error) {
  129. this.addTestResult('❌ 游戏进度事件测试失败: ' + error.message);
  130. }
  131. }
  132. /**
  133. * 测试小程序生命周期管理器
  134. */
  135. private testMPLifecycleManager(): void {
  136. try {
  137. // 测试手动触发显示事件
  138. MPLifecycle.triggerShow({
  139. scene: 1001,
  140. query: '{"manual": "test"}'
  141. });
  142. // 测试手动触发隐藏事件
  143. MPLifecycle.triggerHide();
  144. // 测试获取会话时长
  145. const sessionDuration = MPLifecycle.getSessionDuration();
  146. console.log('[AnalyticsTest] 当前会话时长:', sessionDuration);
  147. this.addTestResult('✅ 小程序生命周期管理器测试通过');
  148. } catch (error) {
  149. this.addTestResult('❌ 小程序生命周期管理器测试失败: ' + error.message);
  150. }
  151. }
  152. /**
  153. * 添加测试结果
  154. */
  155. private addTestResult(result: string): void {
  156. this.testResults.push(result);
  157. console.log('[AnalyticsTest]', result);
  158. }
  159. /**
  160. * 显示测试结果
  161. */
  162. private showTestResults(): void {
  163. const summary = this.testResults.join('\n');
  164. const passedCount = this.testResults.filter(r => r.indexOf('✅') !== -1).length;
  165. const failedCount = this.testResults.filter(r => r.indexOf('❌') !== -1).length;
  166. const finalStatus = `埋点测试完成\n通过: ${passedCount} 项\n失败: ${failedCount} 项\n\n${summary}`;
  167. this.updateStatus(finalStatus);
  168. console.log('[AnalyticsTest] 测试完成');
  169. console.log('[AnalyticsTest] 通过:', passedCount, '项');
  170. console.log('[AnalyticsTest] 失败:', failedCount, '项');
  171. }
  172. /**
  173. * 更新状态显示
  174. */
  175. private updateStatus(status: string): void {
  176. if (this.statusLabel) {
  177. this.statusLabel.string = status;
  178. }
  179. console.log('[AnalyticsTest]', status);
  180. }
  181. /**
  182. * 单独测试某个功能(供外部调用)
  183. */
  184. public testSpecificFeature(featureName: string): void {
  185. switch (featureName) {
  186. case 'basic':
  187. this.testBasicTracking();
  188. break;
  189. case 'lifecycle':
  190. this.testMPLifecycleEvents();
  191. break;
  192. case 'scene':
  193. this.testSceneLoadEvents();
  194. break;
  195. case 'progress':
  196. this.testGameProgressEvents();
  197. break;
  198. case 'manager':
  199. this.testMPLifecycleManager();
  200. break;
  201. default:
  202. console.warn('[AnalyticsTest] 未知的测试功能:', featureName);
  203. }
  204. }
  205. }
  206. // 导出测试工具函数,供控制台调用
  207. (window as any).testAnalytics = {
  208. // 快速测试所有功能
  209. runAll: () => {
  210. const testComponent = new AnalyticsTest();
  211. testComponent.runAllTests();
  212. },
  213. // 测试特定功能
  214. test: (feature: string) => {
  215. const testComponent = new AnalyticsTest();
  216. testComponent.testSpecificFeature(feature);
  217. },
  218. // 手动发送测试事件
  219. sendTestEvent: (eventName: string, properties: any = {}) => {
  220. Analytics.track(eventName, properties);
  221. },
  222. // 获取会话时长
  223. getSessionDuration: () => {
  224. return MPLifecycle.getSessionDuration();
  225. }
  226. };