AppflyerEvent.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. namespace App\Http\AppFlyerEvent;
  3. use Illuminate\Support\Facades\DB;
  4. use Illuminate\Support\Facades\Log;
  5. class AppflyerEvent
  6. {
  7. const Token = 'aoRrdANBXaid2HX77FxE6P';
  8. /**
  9. * @param $user_id
  10. * @param $appsflyer_id
  11. * @param string $appsflyer_event
  12. * @param int $value
  13. * @return mixed
  14. */
  15. public function event($user_id, $appsflyer_id, $appsflyer_event = '', $value = 0)
  16. {
  17. $packag_name = DB::connection('write')->table('QPRecordDB.dbo.RecordPackageName')
  18. ->where('UserID', $user_id)
  19. ->select('PackgeName')
  20. ->first()->PackgeName ?? '';
  21. // 充值用
  22. if (empty($appsflyer_id)) {
  23. $appsflyer_id = DB::connection('write')->table('QPRecordDB.dbo.RecordUserIDAdid')
  24. ->where('UserID', $user_id)
  25. ->select('adid')
  26. ->first()->adid ?? '';
  27. }
  28. if (empty($appsflyer_id)) {
  29. return 'false';
  30. }
  31. $purchase_event = array(
  32. 'appsflyer_id' => $appsflyer_id,
  33. );
  34. $purchase_event['eventName'] = $appsflyer_event;
  35. if ($appsflyer_event == 'af_purchase_new') {
  36. $data['af_revenue'] = $value;
  37. $data['af_currency'] = 'INR';
  38. $purchase_event['eventValue'] = \GuzzleHttp\json_encode($data);
  39. }
  40. $data_string = json_encode($purchase_event);
  41. if (is_string($data_string)) {
  42. Log::channel('AppflyerEvent')->info('af请求数据:' . $data_string);
  43. }
  44. $res = $this->curl($packag_name, $data_string);
  45. Log::channel('AppflyerEvent')->info('af返回结果:' . $res);
  46. // 中大R事件
  47. //$this->zdR($user_id, $appsflyer_id, $value, $packag_name);
  48. return $res;
  49. }
  50. // 中大R事件
  51. public function zdR($user_id, $appsflyer_id, $value)
  52. {
  53. $packag_name = DB::connection('write')->table('QPRecordDB.dbo.RecordPackageName')
  54. ->where('UserID', $user_id)
  55. ->select('PackgeName')
  56. ->first()->PackgeName ?? '';
  57. // 充值用
  58. if (empty($appsflyer_id)) {
  59. $appsflyer_id = DB::connection('write')->table('QPRecordDB.dbo.RecordUserIDAdid')
  60. ->where('UserID', $user_id)
  61. ->select('adid')
  62. ->first()->adid ?? '';
  63. }
  64. if (empty($appsflyer_id)) {
  65. return 'false';
  66. }
  67. // 传中大R事件
  68. $first = DB::connection('write')->table('QPRecordDB.dbo.RecordUserTotalStatistics')
  69. ->where('UserID', $user_id)
  70. ->select('Recharge')
  71. ->first();
  72. if ($first) {
  73. $Recharge = (int)$first->Recharge;
  74. if ($Recharge > 0 && $Recharge < 1000) {
  75. $af_purchase_r1000['appsflyer_id'] = $appsflyer_id;
  76. $af_purchase_r1000['eventName'] = 'af_purchase_r1000';
  77. $af_purchase_r1000Data['af_revenue'] = $value;
  78. $af_purchase_r1000Data['af_currency'] = 'INR';
  79. $af_purchase_r1000['eventValue'] = \GuzzleHttp\json_encode($af_purchase_r1000Data);
  80. $data_string = json_encode($af_purchase_r1000);
  81. if (is_string($data_string)) {
  82. Log::channel('AppflyerEvent')->info($packag_name.'af小于1000 请求数据:' . $data_string);
  83. }
  84. $r = $this->curl($packag_name, $data_string);
  85. Log::channel('AppflyerEvent')->info($packag_name.'af小于1000返回结果:' . $r);
  86. }
  87. if ($Recharge >= 1000) {
  88. $af_purchase_Rr['appsflyer_id'] = $appsflyer_id;
  89. $af_purchase_Rr['eventName'] = 'af_purchase_Rr';
  90. $af_purchase_RrData['af_revenue'] = $value;
  91. $af_purchase_RrData['af_currency'] = 'INR';
  92. $af_purchase_Rr['eventValue'] = \GuzzleHttp\json_encode($af_purchase_RrData);
  93. $data_string = json_encode($af_purchase_Rr);
  94. if (is_string($data_string)) {
  95. Log::channel('AppflyerEvent')->info($packag_name.'af中R请求数据:' . $data_string);
  96. }
  97. $r = $this->curl($packag_name, $data_string);
  98. Log::channel('AppflyerEvent')->info($packag_name.'af中R返回结果:' . $r);
  99. }
  100. if ($Recharge >= 10000) {
  101. $af_purchase_R['appsflyer_id'] = $appsflyer_id;
  102. $af_purchase_R['eventName'] = 'af_purchase_R';
  103. $af_purchase_RData['af_revenue'] = $value;
  104. $af_purchase_RData['af_currency'] = 'INR';
  105. $af_purchase_R['eventValue'] = \GuzzleHttp\json_encode($af_purchase_RData);
  106. $data_string = json_encode($af_purchase_R);
  107. if (is_string($data_string)) {
  108. Log::channel('AppflyerEvent')->info($packag_name.'af大R请求数据:' . $data_string);
  109. }
  110. $r = $this->curl($packag_name, $data_string);
  111. Log::channel('AppflyerEvent')->info($packag_name.'af大R返回结果:' . $r);
  112. }
  113. }
  114. // 当日注册当日充值的玩家的付费事件
  115. $AccountsInfo = DB::connection('write')->table('QPAccountsDB.dbo.AccountsInfo as ai')
  116. ->join('agent.dbo.order as o','ai.UserID','=','o.user_id')
  117. ->where('ai.UserID',$user_id)
  118. ->lock('with(nolock)')
  119. ->where('pay_status',1)
  120. ->whereDate('RegisterDate',date('Y-m-d'))
  121. // ->whereDate('RegisterDate','2021-11-01')
  122. ->count();
  123. if ($AccountsInfo == 1) {
  124. $af_purchase_Registration['appsflyer_id'] = $appsflyer_id;
  125. $af_purchase_Registration['eventName'] = 'af_purchase_Registrationday';
  126. $af_purchase_RegistrationData['af_revenue'] = $value;
  127. $af_purchase_RegistrationData['af_currency'] = 'INR';
  128. $af_purchase_Registration['eventValue'] = \GuzzleHttp\json_encode($af_purchase_RegistrationData);
  129. $data_string = json_encode($af_purchase_Registration);
  130. if (is_string($data_string)) {
  131. Log::channel('AppflyerEvent')->info($packag_name.'当日注册当日充值的玩家的付费事件 请求数据:' . $data_string);
  132. }
  133. $r = $this->curl($packag_name, $data_string);
  134. Log::channel('AppflyerEvent')->info($packag_name.'当日注册当日充值的玩家的付费事件:' . $r);
  135. }
  136. // 所有玩家(包括以前注册的玩家)首次充值的付费事件 af_purchase_First charge
  137. $order = DB::connection('write')->table('agent.dbo.order')
  138. ->where('user_id',$user_id)
  139. ->where('pay_status',1)
  140. ->count();
  141. if ($order == 1) {
  142. $af_purchase_First['appsflyer_id'] = $appsflyer_id;
  143. $af_purchase_First['eventName'] = 'af_purchase_Firstcharge';
  144. $af_purchase_FirstData['af_revenue'] = $value;
  145. $af_purchase_FirstData['af_currency'] = 'INR';
  146. $af_purchase_First['eventValue'] = \GuzzleHttp\json_encode($af_purchase_FirstData);
  147. $data_string = json_encode($af_purchase_First);
  148. if (is_string($data_string)) {
  149. Log::channel('AppflyerEvent')->info($packag_name.'所有玩家(包括以前注册的玩家)首次充值的付费事件 请求数据:' . $data_string);
  150. }
  151. $r = $this->curl($packag_name, $data_string);
  152. Log::channel('AppflyerEvent')->info($packag_name.'所有玩家(包括以前注册的玩家)首次充值的付费事件:' . $r);
  153. }
  154. }
  155. public function curl($packag_name, $data_string,$devKey= null)
  156. {
  157. $url = "https://api2.appsflyer.com/inappevent/" . $packag_name;
  158. $ch = curl_init($url);
  159. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  160. curl_setopt($ch, CURLOPT_HEADER, true);
  161. curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
  162. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  163. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  164. 'Content-Type: application/json',
  165. 'authentication:' . ($devKey??self::Token),
  166. 'Content-Length: ' . strlen($data_string))
  167. );
  168. curl_exec($ch);
  169. $res = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  170. curl_close($ch);
  171. return $res;
  172. }
  173. }