1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TheIconic\Tracking\GoogleAnalytics\Network; |
4
|
|
|
|
5
|
|
|
use TheIconic\Tracking\GoogleAnalytics\Parameters\General\CacheBuster; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class PrepareUrl |
9
|
|
|
* |
10
|
|
|
* @package TheIconic\Tracking\GoogleAnalytics |
11
|
|
|
*/ |
12
|
|
|
class PrepareUrl |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var array |
16
|
|
|
*/ |
17
|
|
|
private $payloadParameters; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
private $cacheBuster = ''; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Build URL which is sent to Google Analytics |
26
|
|
|
* |
27
|
|
|
* @param string $url |
28
|
|
|
* @param SingleParameter[] $singleParameters |
29
|
|
|
* @param CompoundParameterCollection[] $compoundParameters |
30
|
|
|
* @return string |
31
|
|
|
*/ |
32
|
|
|
public function build($url, array $singleParameters, array $compoundParameters) |
33
|
|
|
{ |
34
|
|
|
$singlesPost = $this->getSingleParametersPayload($singleParameters); |
35
|
|
|
|
36
|
|
|
$compoundsPost = $this->getCompoundParametersPayload($compoundParameters); |
37
|
|
|
|
38
|
|
|
$this->payloadParameters = array_merge($singlesPost, $compoundsPost); |
39
|
|
|
|
40
|
|
|
if (!empty($this->cacheBuster)) { |
41
|
|
|
$this->payloadParameters['z'] = $this->cacheBuster; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return $url . '?' . http_build_query($this->payloadParameters); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @return array |
49
|
|
|
*/ |
50
|
|
|
public function getPayloadParameters() |
51
|
|
|
{ |
52
|
|
|
return $this->payloadParameters; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Prepares all the Single Parameters to be sent to GA. |
57
|
|
|
* |
58
|
|
|
* @param SingleParameter[] $singleParameters |
59
|
|
|
* @return array |
60
|
|
|
*/ |
61
|
|
|
private function getSingleParametersPayload(array $singleParameters) |
62
|
|
|
{ |
63
|
|
|
$postData = []; |
64
|
|
|
$cacheBuster = new CacheBuster(); |
65
|
|
|
|
66
|
|
|
foreach ($singleParameters as $parameterObj) { |
67
|
|
|
if ($parameterObj->getName() === $cacheBuster->getName()) { |
68
|
|
|
$this->cacheBuster = $parameterObj->getValue(); |
69
|
|
|
continue; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$postData[$parameterObj->getName()] = $parameterObj->getValue(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $postData; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Prepares compound parameters inside collections to be sent to GA. |
80
|
|
|
* |
81
|
|
|
* @param CompoundParameterCollection[] $compoundParameters |
82
|
|
|
* @return array |
83
|
|
|
*/ |
84
|
|
|
private function getCompoundParametersPayload(array $compoundParameters) |
85
|
|
|
{ |
86
|
|
|
$postData = []; |
87
|
|
|
|
88
|
|
|
foreach ($compoundParameters as $compoundCollection) { |
89
|
|
|
$parameterArray = $compoundCollection->getParametersArray(); |
90
|
|
|
$postData = array_merge($postData, $parameterArray); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $postData; |
94
|
|
|
} |
95
|
|
|
} |