|
1
|
|
|
<?php namespace Arcanedev\LaravelTracker\Trackers; |
|
2
|
|
|
|
|
3
|
|
|
use Arcanedev\LaravelTracker\Contracts\Parsers\RefererParser; |
|
4
|
|
|
use Arcanedev\LaravelTracker\Contracts\Trackers\RefererTracker as RefererTrackerContract; |
|
5
|
|
|
use Arcanedev\LaravelTracker\Support\BindingManager; |
|
6
|
|
|
use Illuminate\Support\Arr; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class RefererTracker |
|
10
|
|
|
* |
|
11
|
|
|
* @package Arcanedev\LaravelTracker\Trackers |
|
12
|
|
|
* @author ARCANEDEV <[email protected]> |
|
13
|
|
|
*/ |
|
14
|
|
|
class RefererTracker extends AbstractTracker implements RefererTrackerContract |
|
15
|
|
|
{ |
|
16
|
|
|
/* ----------------------------------------------------------------- |
|
17
|
|
|
| Getters and Setters |
|
18
|
|
|
| ----------------------------------------------------------------- |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Get the model. |
|
23
|
|
|
* |
|
24
|
|
|
* @return \Arcanedev\LaravelTracker\Models\Referer |
|
25
|
|
|
*/ |
|
26
|
3 |
|
protected function getModel() |
|
27
|
|
|
{ |
|
28
|
3 |
|
return $this->makeModel(BindingManager::MODEL_REFERER); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @return \Arcanedev\LaravelTracker\Contracts\Parsers\RefererParser |
|
33
|
|
|
*/ |
|
34
|
9 |
|
private function getRefererParser() |
|
35
|
|
|
{ |
|
36
|
9 |
|
return $this->make(RefererParser::class); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/* ----------------------------------------------------------------- |
|
40
|
|
|
| Main Methods |
|
41
|
|
|
| ----------------------------------------------------------------- |
|
42
|
|
|
*/ |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Track the referer and return the id. |
|
46
|
|
|
* |
|
47
|
|
|
* @param string $refererUrl |
|
48
|
|
|
* @param string $pageUrl |
|
49
|
|
|
* |
|
50
|
|
|
* @return int|null |
|
51
|
|
|
*/ |
|
52
|
9 |
|
public function track($refererUrl, $pageUrl) |
|
53
|
|
|
{ |
|
54
|
9 |
|
$firstParsed = $this->getRefererParser()->parseUrl($refererUrl); |
|
55
|
|
|
|
|
56
|
9 |
|
if ($firstParsed) { |
|
|
|
|
|
|
57
|
|
|
$attributes = [ |
|
58
|
3 |
|
'url' => $firstParsed['url'], |
|
59
|
3 |
|
'host' => $firstParsed['host'], |
|
60
|
3 |
|
'domain_id' => $this->trackDomain($firstParsed['domain']), |
|
61
|
|
|
'medium' => null, |
|
62
|
|
|
'source' => null, |
|
63
|
|
|
'search_terms_hash' => null, |
|
64
|
|
|
]; |
|
65
|
|
|
|
|
66
|
3 |
|
$secondParsed = $this->getRefererParser()->parse($firstParsed['url'], $pageUrl); |
|
67
|
|
|
|
|
68
|
3 |
|
if ($secondParsed->isKnown()) { |
|
69
|
3 |
|
$attributes['medium'] = $secondParsed->getMedium(); |
|
70
|
3 |
|
$attributes['source'] = $secondParsed->getSource(); |
|
71
|
3 |
|
$attributes['search_terms_hash'] = sha1($secondParsed->getSearchTerm()); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** @var \Arcanedev\LaravelTracker\Models\Referer $referer */ |
|
75
|
3 |
|
$referer = $this->getModel()->newQuery()->firstOrCreate( |
|
76
|
3 |
|
Arr::only($attributes, ['url', 'search_terms_hash']), |
|
77
|
2 |
|
$attributes |
|
78
|
|
|
); |
|
79
|
|
|
|
|
80
|
3 |
|
if ($secondParsed->isKnown()) { |
|
81
|
3 |
|
$this->trackRefererSearchTerms($referer, $secondParsed->getSearchTerm()); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
3 |
|
return $referer->id; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
6 |
|
return null; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Track the domain and get the id. |
|
92
|
|
|
* |
|
93
|
|
|
* @param string $name |
|
94
|
|
|
* |
|
95
|
|
|
* @return int |
|
96
|
|
|
*/ |
|
97
|
3 |
|
private function trackDomain($name) |
|
98
|
|
|
{ |
|
99
|
3 |
|
return $this->makeModel(BindingManager::MODEL_DOMAIN) |
|
100
|
3 |
|
->newQuery() |
|
101
|
3 |
|
->firstOrCreate(compact('name')) |
|
102
|
3 |
|
->getKey(); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Store the referer's search terms. |
|
107
|
|
|
* |
|
108
|
|
|
* @param \Arcanedev\LaravelTracker\Contracts\Models\Referer $referer |
|
109
|
|
|
* @param string $searchTerms |
|
110
|
|
|
*/ |
|
111
|
3 |
|
private function trackRefererSearchTerms($referer, $searchTerms) |
|
112
|
|
|
{ |
|
113
|
3 |
|
$terms = []; |
|
114
|
|
|
|
|
115
|
3 |
|
foreach (explode(' ', $searchTerms) as $term) { |
|
116
|
3 |
|
$terms[] = $this->makeModel(BindingManager::MODEL_REFERER_SEARCH_TERM)->fill([ |
|
117
|
3 |
|
'search_term' => $term, |
|
118
|
|
|
]); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
3 |
|
if (count($terms) > 0) |
|
122
|
3 |
|
$referer->searchTerms()->saveMany($terms); |
|
123
|
|
|
|
|
124
|
3 |
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.