WebUtmService.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. namespace App\Web\Services;
  3. use App\Services\CommonUserBaseService;
  4. use App\Web\Models\WebUtmModel;
  5. use Illuminate\Support\Facades\Cache;
  6. class WebUtmService extends CommonUserBaseService
  7. {
  8. protected $cache = true;
  9. protected $cacheBucket = 'WebUtm:';
  10. public function __construct(WebUtmModel $model)
  11. {
  12. $this->model = $model;
  13. }
  14. public function getUtm($id)
  15. {
  16. return $this->model->getWebUtm($id);
  17. }
  18. public function delUtm($ids)
  19. {
  20. return $this->model->delWebUtm($ids);
  21. }
  22. public function addOrUpdUtm($params)
  23. {
  24. $userId = $this->getAuthUserId() ?? 0;
  25. if (!empty($id)) {
  26. $this->model->updWebUtm($id, $params);
  27. } else {
  28. $oldUtm = $this->model->getUtmByParams($params);
  29. if (!empty($oldUtm)) {
  30. $id = $oldUtm['id'];
  31. $this->model->updWebUtm($id, $params);
  32. } else {
  33. $id = $this->model->addWebUtm($userId, $params);
  34. }
  35. }
  36. return $id;
  37. }
  38. public function getWebUtmList($campaignName = '', $campaignSource = '', $campaignMedium = '', $pageSize = 10, $orderBy = '', $orderDirect = '')
  39. {
  40. return $this->model->getWebUtmList($campaignName, $campaignSource, $campaignMedium, $pageSize, $orderBy, $orderDirect);
  41. }
  42. /**
  43. * UTM缓存pv key获取方式
  44. * @param $id
  45. * @return string
  46. */
  47. public function getUtmPvCacheKey($id)
  48. {
  49. return $this->cacheBucket . '_web_utm_pv_' . $id;
  50. }
  51. /**
  52. * UTM缓存uv key获取方式
  53. * @param $id
  54. * @return string
  55. */
  56. public function getUtmUvCacheKey($id)
  57. {
  58. return $this->cacheBucket . '_web_utm_uv_' . $id;
  59. }
  60. /**
  61. * UTM IP 缓存key获取方式
  62. * @param $id
  63. * @param $ip
  64. * @return string
  65. */
  66. public function getUtmIpCacheKey($id, $ip)
  67. {
  68. return $this->cacheBucket . '_web_utm_' . $id . '_' . $ip;
  69. }
  70. /**
  71. * @return string
  72. */
  73. public function getUtmCacheIdsKey()
  74. {
  75. return $this->cacheBucket . '_web_utm_cache_list';
  76. }
  77. /**
  78. * 广告统计分析
  79. * @param $utmParams
  80. * @param $clientIp
  81. * @param $clientHost
  82. * @param $clientPath
  83. * @return int
  84. */
  85. public function statisticAnalysis($utmParams, $clientIp, $clientHost, $clientPath)
  86. {
  87. //判断是否广告渠道进入的用户
  88. if(!empty($utmParams)) {
  89. $utmConstParams = ['campaign_id', 'campaign_name', 'campaign_source', 'campaign_term',
  90. 'campaign_medium', 'campaign_content'];
  91. $utmKeys = array_keys($utmParams);
  92. $leadArr = array_intersect($utmKeys, $utmConstParams);
  93. if (!empty($leadArr)) {
  94. $utm = $this->model->getUtmByParams($utmParams);
  95. if(!empty($utm)) {
  96. $parsedUrl = parse_url($utm['website_url']);
  97. if (!empty($parsedUrl['host']) && !empty($parsedUrl['path'])) {
  98. $rawClientPath = str_replace('/', '', $clientPath);
  99. $rawParsedPath = str_replace('/', '', $parsedUrl['path']);
  100. if ($clientHost == $parsedUrl['host'] &&
  101. (empty($rawClientPath) && empty($rawParsedPath)
  102. || $rawClientPath == $rawParsedPath)
  103. ) {
  104. $pvCache = Cache::get($this->getUtmPvCacheKey($utm['id']));
  105. if ($pvCache) {
  106. $pvCache = $pvCache + 1;
  107. } else {
  108. $pvCache = 1;
  109. }
  110. Cache::put($this->getUtmPvCacheKey($utm['id']), $pvCache, 24 * 3600);
  111. // if ($clientIp) {
  112. // $ipCache = Cache::get($this->getUtmIpCacheKey($utm['id'], $clientIp));
  113. // if (empty($ipCache)) {
  114. // $uvCache = Cache::get($this->getUtmUvCacheKey($utm['id']));
  115. // if ($uvCache) {
  116. // $uvCache = $uvCache + 1;
  117. // } else {
  118. // $uvCache = 1;
  119. // }
  120. // Cache::put($this->getUtmUvCacheKey($utm['id']), $uvCache, 24 * 3600);
  121. // Cache::put($this->getUtmIpCacheKey($utm['id'], $clientIp), 1, 24*3600);
  122. // }
  123. // }
  124. }
  125. }
  126. }
  127. }
  128. }
  129. return 1;
  130. }
  131. /**
  132. * 持久化存储Pv,Uv
  133. * @param $page
  134. * @return int
  135. */
  136. public function persistenceUtmPvUv($page = 1)
  137. {
  138. if (config('app.is_utm')) {
  139. $pageSize = 999;
  140. $data = $this->model->getWebUtmByPage($page, $pageSize);
  141. if (!empty($data)) {
  142. foreach ($data as $utm) {
  143. $pvCache = Cache::get($this->getUtmPvCacheKey($utm['id']));
  144. // $uvCache = Cache::get($this->getUtmUvCacheKey($utm['id']));
  145. if (!empty($pvCache)) {
  146. $this->model->where('id', $utm['id'])->increment('pv', $pvCache);
  147. Cache::forget($this->getUtmPvCacheKey($utm['id']));
  148. }
  149. // if (!empty($uvCache)) {
  150. // $this->model->increment('uv', $uvCache);
  151. // Cache::forget($this->getUtmUvCacheKey($utm['id']));
  152. // }
  153. }
  154. $page = $page + 1;
  155. $this->persistenceUtmPvUv($page);
  156. }
  157. return 1;
  158. }
  159. return 0;
  160. }
  161. }