TiktokEventReportService.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. namespace App\Service\Callback\Tiktok;
  3. use Exception;
  4. use GuzzleHttp\Client;
  5. /**
  6. * 抖音数据回调---新版---使用事件管理模式
  7. * ios加桌和普通抖音事件管理是一个回传逻辑
  8. * 开发文档:https://event-manager.oceanengine.com/docs/8650/h5_api_docs/
  9. */
  10. class TiktokEventReportService
  11. {
  12. /**
  13. * 充值用户事件类型
  14. */
  15. protected $charge_event_type = 'active_pay';
  16. /**
  17. * 注册用户事件类型
  18. */
  19. protected $register_event_type = 'active_register';
  20. /**
  21. * 激活事件类型
  22. */
  23. protected $active_event_type = 'active';
  24. /**
  25. * 次留事件类型
  26. */
  27. protected $rentention_event_type = 'next_day_open';
  28. /**
  29. * 付费ROI事件类型
  30. */
  31. protected $purchase_roi_event_type = 'purchase_roi';
  32. /**
  33. * 充值用户数据上报地址
  34. */
  35. protected $report_url = 'https://analytics.oceanengine.com/api/v2/conversion';
  36. private $user;
  37. public function __construct()
  38. {
  39. }
  40. public function roiReportCharge($user) {
  41. return $this->report($this->report_url, [
  42. 'event_type' => $this->purchase_roi_event_type,
  43. 'context' => [
  44. 'ad' => [
  45. 'callback' => $user->callback
  46. ]
  47. ],
  48. 'timestamp' => time()
  49. ]);
  50. }
  51. /**
  52. * 付费上报
  53. */
  54. public function reportCharge($user, $amount=0)
  55. {
  56. return $this->report($this->report_url, [
  57. 'event_type' => $this->charge_event_type,
  58. 'context' => [
  59. 'ad' => [
  60. 'callback' => $user->callback
  61. ]
  62. ],
  63. 'timestamp' => time()
  64. ]);
  65. }
  66. /**
  67. * 注册上报
  68. */
  69. public function reportRegister(DouyinUser $user)
  70. {
  71. return $this->report($this->report_url, [
  72. 'event_type' => $this->register_event_type,
  73. 'context' => [
  74. 'ad' => [
  75. 'callback' => $user->callback
  76. ]
  77. ],
  78. 'timestamp' => time()
  79. ]);
  80. }
  81. /**
  82. * 次日留存上报
  83. */
  84. public function reportRentention()
  85. {
  86. return $this->report($this->report_url, [
  87. 'event_type' => $this->rentention_event_type,
  88. 'context' => [
  89. 'ad' => [
  90. 'callback' => $this->user->callback
  91. ]
  92. ],
  93. 'timestamp' => time()
  94. ]);
  95. }
  96. public function reportAddDesktop()
  97. {
  98. return [];
  99. }
  100. /**
  101. * 激活上报
  102. */
  103. public function reportActive($user)
  104. {
  105. return $this->report($this->report_url, [
  106. 'event_type' => $this->active_event_type,
  107. 'context' => [
  108. 'ad' => [
  109. 'callback' => $user->callback
  110. ]
  111. ],
  112. 'timestamp' => time()
  113. ]);
  114. }
  115. /**
  116. * 数据上报
  117. */
  118. public function report(string $url, array $query_params)
  119. {
  120. $result = false;
  121. $content = '';
  122. try {
  123. myLog('tiktok_event_report')->info('report_param_event_tiktok:' . json_encode($query_params));
  124. $client = new Client(['timeout' => 4]);
  125. $response = $client->request('post', $url, ['json' => $query_params]);
  126. $content = $response->getBody()->getContents();
  127. myLog('tiktok_event_report')->info('report_result_event_tiktok:' . json_encode($content, true));
  128. $status_code = $response->getStatusCode();
  129. if ($status_code == 200) {
  130. $result = true;
  131. } else {
  132. $result = false;
  133. }
  134. } catch (Exception $e) {
  135. myLog('tiktok_event_report')->error('event_tiktok_ept:' . json_encode($query_params) . ' ept:' . $e->getMessage());
  136. } finally {
  137. $query_params['report_url'] = $url;
  138. return [
  139. 'result' => $result,
  140. 'content' => $content,
  141. 'query_params' => $query_params,
  142. ];
  143. }
  144. }
  145. }