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