Welcome to mirror list, hosted at ThFree Co, Russian Federation.

GoalsRequestProcessor.php « Tracker « Goals « plugins - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2bde9479283b914fc1ffc6889b10c6eb38b1c933 (plain)
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
<?php
/**
 * Piwik - free/libre analytics platform
 *
 * @link http://piwik.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */

namespace Piwik\Plugins\Goals\Tracker;

use Piwik\Common;
use Piwik\Tracker\Action;
use Piwik\Tracker\GoalManager;
use Piwik\Tracker\Request;
use Piwik\Tracker\RequestProcessor;
use Piwik\Tracker\Visit\VisitProperties;

/**
 * TODO
 *
 * TODO: document request metadata here
 */
class GoalsRequestProcessor extends RequestProcessor
{
    /**
     * TODO: GoalManager should be stateless and stored in DI.
     *
     * @var GoalManager
     */
    public static $goalManager = null;

    public function processRequestParams(VisitProperties $visitProperties, Request $request)
    {
        self::$goalManager = new GoalManager($request);

        if (self::$goalManager->isManualGoalConversion()) {
            // this request is from the JS call to piwikTracker.trackGoal()
            $someGoalsConverted = self::$goalManager->detectGoalId($request->getIdSite());

            $visitProperties->setRequestMetadata('Goals', 'someGoalsConverted', $someGoalsConverted);
            $visitProperties->setRequestMetadata('Goals', 'visitIsConverted', $someGoalsConverted);

            $visitProperties->setRequestMetadata('Actions', 'action', null); // don't track actions when doing manual goal conversions

            // if we find a idgoal in the URL, but then the goal is not valid, this is most likely a fake request
            if (!$someGoalsConverted) {
                Common::printDebug('Invalid goal tracking request for goal id = ' . self::$goalManager->idGoal);
                return true;
            }
        }

        return false;
    }

    public function manipulateVisitProperties(VisitProperties $visitProperties, Request $request)
    {
        $visitsConverted = $visitProperties->getRequestMetadata('Goals', 'visitIsConverted');

        /** @var Action $action */
        $action = $visitProperties->getRequestMetadata('Actions', 'action');

        // if the visit hasn't already been converted another way (ie, manual goal conversion or ecommerce conversion,
        // try to convert based on the action)
        if (!$visitsConverted
            && $action
        ) {
            $someGoalsConverted = self::$goalManager->detectGoalsMatchingUrl($request->getIdSite(), $action);

            $visitProperties->setRequestMetadata('Goals', 'someGoalsConverted', $someGoalsConverted);
            $visitProperties->setRequestMetadata('Goals', 'visitIsConverted', $someGoalsConverted);
        }
    }

    public function processRequest(VisitProperties $visitProperties)
    {
        $isManualGoalConversion = self::$goalManager->isManualGoalConversion();
        $requestIsEcommerce = self::$goalManager->requestIsEcommerce;

        $visitorNotFoundInDb = $visitProperties->getRequestMetadata('CoreHome', 'visitorNotFoundInDb');

        // There is an edge case when:
        // - two manual goal conversions happen in the same second
        // - which result in handleExistingVisit throwing the exception
        //   because the UPDATE didn't affect any rows (one row was found, but not updated since no field changed)
        // - the exception is caught here and will result in a new visit incorrectly
        // In this case, we cancel the current conversion to be recorded:
        if ($visitorNotFoundInDb
            && ($isManualGoalConversion
                || $requestIsEcommerce)
        ) {
            $visitProperties->setRequestMetadata('Goals', 'someGoalsConverted', false);
            $visitProperties->setRequestMetadata('Goals', 'visitIsConverted', false);
        }
    }
}