File: VisitorDetails.php

package info (click to toggle)
matomo 5.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 95,068 kB
  • sloc: php: 289,425; xml: 127,249; javascript: 112,130; python: 202; sh: 178; makefile: 20; sql: 10
file content (189 lines) | stat: -rw-r--r-- 5,469 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php

/**
 * Matomo - free/libre analytics platform
 *
 * @link    https://matomo.org
 * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */

namespace Piwik\Plugins\Referrers;

use Piwik\Common;
use Piwik\Plugins\Live\VisitorDetailsAbstract;
use Piwik\UrlHelper;
use Piwik\View;

class VisitorDetails extends VisitorDetailsAbstract
{
    public function extendVisitorDetails(&$visitor)
    {
        $visitor['referrerType']              = $this->getReferrerType();
        $visitor['referrerTypeName']          = $this->getReferrerTypeName();
        $visitor['referrerName']              = $this->getReferrerName();
        $visitor['referrerKeyword']           = $this->getKeyword();
        $visitor['referrerKeywordPosition']   = $this->getKeywordPosition();
        $visitor['referrerUrl']               = $this->getReferrerUrl();
        $visitor['referrerSearchEngineUrl']   = $this->getSearchEngineUrl();
        $visitor['referrerSearchEngineIcon']  = $this->getSearchEngineIcon();
        $visitor['referrerSocialNetworkUrl']  = $this->getSocialNetworkUrl();
        $visitor['referrerSocialNetworkIcon'] = $this->getSocialNetworkIcon();
        $visitor['referrerAIAssistantUrl'] = $this->getAIAssistantUrl();
        $visitor['referrerAIAssistantIcon'] = $this->getAIAssistantIcon();
    }

    public function renderVisitorDetails($visitorDetails)
    {
        $view            = new View('@Referrers/_visitorDetails.twig');
        $view->sendHeadersWhenRendering = false;
        $view->visitInfo = $visitorDetails;
        return [[ 10, $view->render() ]];
    }

    public function renderActionTooltip($action, $visitInfo)
    {
        if (
            empty($action['type'])
            || ($action['type'] !== 'goal' && $action['type'] !== 'ecommerceOrder')
            || empty($action['referrerType'])
        ) {
            return [];
        }

        // Attribution information for goals
        $view         = new View('@Referrers/_actionTooltip');
        $view->sendHeadersWhenRendering = false;
        $view->action = $action;
        return [[ 10, $view->render() ]];
    }

    protected function getReferrerType()
    {
        try {
            $referrerType = getReferrerTypeFromShortName($this->details['referer_type']);
        } catch (\Exception $e) {
            $referrerType = '';
        }

        return $referrerType;
    }

    protected function getReferrerTypeName()
    {
        return getReferrerTypeLabel($this->details['referer_type']);
    }

    protected function getKeyword()
    {
        $keyword = Common::unsanitizeInputValue($this->details['referer_keyword']);

        if ($this->getReferrerType() == 'search') {
            $keyword = API::getCleanKeyword($keyword);
        }

        return urldecode($keyword);
    }

    protected function getReferrerUrl()
    {
        if (UrlHelper::isLookLikeUrl($this->details['referer_url'])) {
            return $this->details['referer_url'];
        }

        return null;
    }

    protected function getKeywordPosition()
    {
        if (
            $this->getReferrerType() == 'search'
            && strpos($this->getReferrerName(), 'Google') !== false
        ) {
            $url = @parse_url($this->details['referer_url']);
            if (empty($url['query'])) {
                return null;
            }

            $position = UrlHelper::getParameterFromQueryString($url['query'], 'cd');
            if (!empty($position)) {
                return $position;
            }
        }

        return null;
    }

    protected function getReferrerName(): string
    {
         return html_entity_decode(($this->details['referer_name'] ?? ''), ENT_QUOTES, "UTF-8");
    }

    protected function getSearchEngineUrl()
    {
        if (
            $this->getReferrerType() == 'search'
            && !empty($this->details['referer_name'])
        ) {
            return SearchEngine::getInstance()->getUrlFromName($this->details['referer_name']);
        }

        return null;
    }

    protected function getSearchEngineIcon()
    {
        $searchEngineUrl = $this->getSearchEngineUrl();

        if (!is_null($searchEngineUrl)) {
            return SearchEngine::getInstance()->getLogoFromUrl($searchEngineUrl);
        }

        return null;
    }

    protected function getSocialNetworkUrl()
    {
        if (
            $this->getReferrerType() == 'social'
            && !empty($this->details['referer_name'])
        ) {
            return Social::getInstance()->getMainUrl($this->details['referer_url']);
        }

        return null;
    }

    protected function getSocialNetworkIcon()
    {
        $socialNetworkUrl = $this->getSocialNetworkUrl();

        if (!is_null($socialNetworkUrl)) {
            return Social::getInstance()->getLogoFromUrl($socialNetworkUrl);
        }

        return null;
    }

    protected function getAIAssistantUrl(): ?string
    {
        if (
            $this->getReferrerType() === 'ai'
            && !empty($this->details['referer_name'])
        ) {
            return AIAssistant::getInstance()->getMainUrl($this->details['referer_url']);
        }

        return null;
    }

    protected function getAIAssistantIcon(): ?string
    {
        $aiAssistantUrl = $this->getAIAssistantUrl();

        if (!is_null($aiAssistantUrl)) {
            return AIAssistant::getInstance()->getLogoFromUrl($aiAssistantUrl);
        }

        return null;
    }
}