File size: 6,889 Bytes
5b04ba0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<?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\Goals\tests\System;

use Piwik\API\Request;
use Piwik\Common;
use Piwik\DataTable;
use Piwik\Date;
use Piwik\Tests\Fixtures\ThreeGoalsOnePageview;
use Piwik\Tests\Framework\Fixture;
use Piwik\Tests\Fixtures\TwoSitesEcommerceOrderWithItems;
use Piwik\Tests\Framework\TestCase\SystemTestCase;

/**
 * @group GoalsEcommerceTestTest
 * @group GoalsEcommerceTest
 * @group Plugins
 */
class GoalsEcommerceTest extends SystemTestCase
{
    /**
     * @var ThreeGoalsOnePageview
     */
    public static $fixture = null;

    /**
     * @dataProvider getApiForTesting
     */
    public function testApi($api, $params)
    {
        $this->runApiTests($api, $params);
    }

    public function testSalesByReferrerTypeSeparatesAIAssistantAndCampaignEcommerceOrders(): void
    {
        $this->trackAIAssistantEcommerceOrder('2011-06-03 10:00:00', 'ai-campaign-order-1', 100, 'http://chatgpt.com');
        $this->trackAIAssistantEcommerceOrder('2011-06-05 10:00:00', 'ai-campaign-order-2', 200, 'http://chatgpt.com');
        $this->trackAIAssistantEcommerceOrder('2011-06-05 11:00:00', 'ai-campaign-order-3', 40, 'http://chatgpt.com');
        $this->trackAIAssistantEcommerceOrder('2011-06-03 11:00:00', 'ai-attributed-order-1', 50);
        $this->trackAIAssistantEcommerceOrder('2011-06-05 12:00:00', 'ai-attributed-order-2', 60);

        $report = Request::processRequest('Referrers.getReferrerType', [
            'idSite' => self::$fixture->idSite,
            'period' => 'month',
            'date' => '2011-06-01',
            'idGoal' => 'ecommerceOrder',
            // Load-bearing request params: the two goal filters materialise the
            // goal_ecommerceOrder_* columns asserted below, and _setReferrerTypeLabel=0
            // keeps the row labels as the numeric referrer-type ids matched by the
            // Common::REFERRER_TYPE_* constants in assertSalesByReferrerType(). Do not drop them.
            'filter_update_columns_when_show_all_goals' => 'ecommerceOrder',
            'filter_show_goal_columns_process_goals' => 'ecommerceOrder',
            'apiModule' => 'Referrers',
            'apiAction' => 'getReferrerType',
            '_setReferrerTypeLabel' => 0,
            'format_metrics' => 0,
        ]);

        $this->assertInstanceOf(DataTable::class, $report);
        $this->assertSalesByReferrerType($report);
    }

    private function assertSalesByReferrerType(DataTable $report): void
    {
        $campaignRow = $report->getRowFromLabel(Common::REFERRER_TYPE_CAMPAIGN);
        $aiAssistantRow = $report->getRowFromLabel(Common::REFERRER_TYPE_AI_ASSISTANT);

        $this->assertNotFalse($campaignRow);
        $this->assertNotFalse($aiAssistantRow);

        $this->assertSame(3, (int) $campaignRow->getColumn('goal_ecommerceOrder_nb_conversions'), 'Campaign ecommerce orders');
        $this->assertSame(340.0, (float) $campaignRow->getColumn('goal_ecommerceOrder_revenue'), 'Campaign ecommerce revenue');

        $this->assertSame(2, (int) $aiAssistantRow->getColumn('goal_ecommerceOrder_nb_conversions'), 'AI Assistant ecommerce orders');
        $this->assertSame(110.0, (float) $aiAssistantRow->getColumn('goal_ecommerceOrder_revenue'), 'AI Assistant ecommerce revenue');
    }

    private function trackAIAssistantEcommerceOrder(string $dateTime, string $orderId, float $revenue, ?string $campaignName = null): void
    {
        $tracker = Fixture::getTracker(self::$fixture->idSite, $dateTime, $defaultInit = true);
        $tracker->setNewVisitorId();
        $tracker->setUrl('http://example.org/landing?utm_source=chatgpt.com');
        Fixture::checkResponse($tracker->doTrackPageView('AI landing page'));

        $tracker->setForceVisitDateTime(Date::factory($dateTime)->addHour(0.1)->getDatetime());

        if ($campaignName !== null) {
            $tracker->setCustomTrackingParameter('_rcn', $campaignName);
        }

        Fixture::checkResponse($tracker->doTrackEcommerceOrder($orderId, $revenue));
    }

    public function getApiForTesting()
    {
        return [
            ['Referrers.getReferrerType', [
                'idSite' => self::$fixture->idSite,
                'date' => self::$fixture->dateTime,
                'period' => 'day',
                'testSuffix' => 'showSalesByReferrers',
                'otherRequestParameters' => [
                    'filter_update_columns_when_show_all_goals' => 'ecommerceOrder',
                    'filter_show_goal_columns_process_goals' => 'ecommerceOrder',
                    'apiModule' => 'Referrers',
                    'apiAction' => 'getReferrerType',
                    'idGoal' => 'ecommerceOrder',
                ],
            ]],
            ['Actions.getPageUrls', [
                'idSite' => self::$fixture->idSite,
                'date' => self::$fixture->dateTime,
                'period' => 'day',
                'testSuffix' => 'showSalesByPages',
                'otherRequestParameters' => [
                    'filter_update_columns_when_show_all_goals' => 'ecommerceOrder',
                    'filter_show_goal_columns_process_goals' => 'ecommerceOrder',
                    'apiModule' => 'Actions',
                    'apiAction' => 'getPageUrls',
                    'idGoal' => 'ecommerceOrder',
                ],
            ]],
            ['Goals.getVisitsUntilConversion', [
                'idSite' => self::$fixture->idSite,
                'date' => self::$fixture->dateTime,
                'period' => 'day',
                'testSuffix' => 'showSalesByVisits',
                'otherRequestParameters' => [
                    'apiModule' => 'Goals',
                    'apiAction' => 'getVisitsUntilConversion',
                    'idGoal' => 'ecommerceOrder',
                ],
            ]],
            ['UserCountry.getCountry', [
                'idSite' => self::$fixture->idSite,
                'date' => self::$fixture->dateTime,
                'period' => 'day',
                'testSuffix' => 'showSalesByUserCountry',
                'otherRequestParameters' => [
                    'filter_update_columns_when_show_all_goals' => 'ecommerceOrder',
                    'filter_show_goal_columns_process_goals' => 'ecommerceOrder',
                    'apiModule' => 'UserCountry',
                    'apiAction' => 'getCountry',
                    'idGoal' => 'ecommerceOrder',
                ],
            ]],
        ];
    }

    public static function getOutputPrefix()
    {
        return 'goals_ecommerce';
    }

    public static function getPathToTestDirectory()
    {
        return dirname(__FILE__);
    }
}

GoalsEcommerceTest::$fixture = new TwoSitesEcommerceOrderWithItems();