TiktokEventReportService.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. namespace Modules\Callback\Services;
  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. /**
  41. * 付费上报
  42. */
  43. public function reportCharge($user, $amount=0)
  44. {
  45. return $this->report($this->report_url, [
  46. 'event_type' => $this->charge_event_type,
  47. 'context' => [
  48. 'ad' => [
  49. 'callback' => $user->callback
  50. ]
  51. ],
  52. 'timestamp' => time()
  53. ]);
  54. }
  55. /**
  56. * 注册上报
  57. */
  58. public function reportRegister(DouyinUser $user)
  59. {
  60. return $this->report($this->report_url, [
  61. 'event_type' => $this->register_event_type,
  62. 'context' => [
  63. 'ad' => [
  64. 'callback' => $user->callback
  65. ]
  66. ],
  67. 'timestamp' => time()
  68. ]);
  69. }
  70. /**
  71. * 次日留存上报
  72. */
  73. public function reportRentention()
  74. {
  75. return $this->report($this->report_url, [
  76. 'event_type' => $this->rentention_event_type,
  77. 'context' => [
  78. 'ad' => [
  79. 'callback' => $this->user->callback
  80. ]
  81. ],
  82. 'timestamp' => time()
  83. ]);
  84. }
  85. public function reportAddDesktop()
  86. {
  87. return [];
  88. }
  89. /**
  90. * 激活上报
  91. */
  92. public function reportActive($user)
  93. {
  94. return $this->report($this->report_url, [
  95. 'event_type' => $this->active_event_type,
  96. 'context' => [
  97. 'ad' => [
  98. 'callback' => $user->callback
  99. ]
  100. ],
  101. 'timestamp' => time()
  102. ]);
  103. }
  104. /**
  105. * 数据上报
  106. */
  107. public function report(string $url, array $query_params)
  108. {
  109. $result = false;
  110. $content = '';
  111. try {
  112. myLog('tiktok_event_report')->info('report_param_event_tiktok:' . json_encode($query_params));
  113. $client = new Client(['timeout' => 4]);
  114. $response = $client->request('post', $url, ['json' => $query_params]);
  115. $content = $response->getBody()->getContents();
  116. myLog('tiktok_event_report')->info('report_result_event_tiktok:' . json_encode($content, true));
  117. $status_code = $response->getStatusCode();
  118. if ($status_code == 200) {
  119. $result = true;
  120. } else {
  121. $result = false;
  122. }
  123. } catch (Exception $e) {
  124. myLog('tiktok_event_report')->error('event_tiktok_ept:' . json_encode($query_params) . ' ept:' . $e->getMessage());
  125. } finally {
  126. $query_params['report_url'] = $url;
  127. return [
  128. 'result' => $result,
  129. 'content' => $content,
  130. 'query_params' => $query_params,
  131. ];
  132. }
  133. }
  134. }