Skip to content

Commit 0333135

Browse files
committed
ACP2E-4045: Product Views Report Incorrect - Lower Count Compared to GA
1 parent 7553e02 commit 0333135

File tree

5 files changed

+30
-20
lines changed

5 files changed

+30
-20
lines changed

app/code/Magento/Reports/Controller/Report/Product/View.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
namespace Magento\Reports\Controller\Report\Product;
99

10-
use Magento\Backend\App\Action\Context;
10+
use Magento\Framework\App\Action\Context;
1111
use Magento\Customer\Model\Session;
1212
use Magento\Customer\Model\Visitor;
1313
use Magento\Framework\App\Action\HttpPostActionInterface;

app/code/Magento/Reports/Observer/CatalogProductViewObserver.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2015 Adobe
4+
* All Rights Reserved.
55
*/
66
namespace Magento\Reports\Observer;
77

@@ -11,6 +11,10 @@
1111
/**
1212
* Reports Event observer model
1313
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
14+
* @deprecated We cannot rely on the observer to count product page visits (GET requests)
15+
* because the observer will not be called due fact that pages rely heavily on cache.
16+
* Instead, a call from a product page should be made to a separate controller.
17+
* @see \Magento\Reports\Controller\Report\Product\View
1418
*/
1519
class CatalogProductViewObserver implements ObserverInterface
1620
{

app/code/Magento/Reports/etc/frontend/events.xml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?xml version="1.0"?>
22
<!--
33
/**
4-
* Copyright © Magento, Inc. All rights reserved.
5-
* See COPYING.txt for license details.
4+
* Copyright 2013 Adobe
5+
* All Rights Reserved.
66
*/
77
-->
88
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
@@ -15,9 +15,6 @@
1515
<event name="customer_logout">
1616
<observer name="reports" instance="Magento\Reports\Observer\CustomerLogoutObserver" />
1717
</event>
18-
<event name="catalog_controller_product_view">
19-
<observer name="reports" instance="Magento\Reports\Observer\CatalogProductViewObserver" />
20-
</event>
2118
<event name="sendfriend_product">
2219
<observer name="reports" instance="Magento\Reports\Observer\SendfriendProductObserver" />
2320
</event>

app/code/Magento/Reports/view/frontend/web/js/product-viewed.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44
*/
55

66
define([
7-
'jquery'
8-
], function ($) {
7+
'jquery',
8+
'mage/url'
9+
], function ($, urlBuilder) {
910
'use strict';
1011

1112
return function (data) {
1213
$.ajax({
13-
url: 'reports/report_product/view',
14+
url: urlBuilder.build('reports/report_product/view'),
1415
type: 'POST',
1516
data: {'product_id': data.product_id },
1617
dataType: 'json'
Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2017 Adobe
4+
* All Rights Reserved.
55
*/
6-
namespace Magento\Backend\Block\Dashboard\Tab\Products;
6+
namespace Magento\Reports\Controller\Adminhtml\Report;
77

8-
use Magento\TestFramework\Helper\Bootstrap as BootstrapHelper;
9-
use Magento\Framework\ObjectManagerInterface;
10-
use Magento\Framework\View\LayoutInterface;
118
use Magento\Backend\Block\Dashboard\Tab\Products\Viewed as ViewedProductsTabBlock;
129
use Magento\Catalog\Model\ProductRepository;
10+
use Magento\Framework\App\Request\Http;
1311
use Magento\Framework\Event\ManagerInterface as EventManager;
12+
use Magento\Framework\ObjectManagerInterface;
13+
use Magento\Framework\View\LayoutInterface;
14+
use Magento\TestFramework\Helper\Bootstrap as BootstrapHelper;
15+
use function PHPUnit\Framework\assertEquals;
1416

1517
/**
1618
* @magentoAppArea frontend
1719
*/
18-
class ViewedTest extends \PHPUnit\Framework\TestCase
20+
class ViewedTest extends \Magento\TestFramework\TestCase\AbstractController
1921
{
2022
/**
2123
* @var ObjectManagerInterface
@@ -39,24 +41,30 @@ class ViewedTest extends \PHPUnit\Framework\TestCase
3941

4042
protected function setUp(): void
4143
{
44+
parent::setUp();
4245
$this->objectManager = BootstrapHelper::getObjectManager();
4346
$this->layout = $this->objectManager->get(LayoutInterface::class);
4447
$this->productRepository = $this->objectManager->get(ProductRepository::class);
4548
$this->eventManager = $this->objectManager->get(EventManager::class);
4649
}
4750

4851
/**
52+
* Assert viewed product in reports.
53+
*
4954
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
5055
* @magentoDbIsolation enabled
5156
* @magentoAppIsolation enabled
5257
* @magentoConfigFixture default/reports/options/enabled 1
5358
*/
5459
public function testGetPreparedCollectionProductPrice()
5560
{
61+
$sku = 'simple';
62+
$product = $this->productRepository->get($sku);
63+
$this->getRequest()->setMethod(Http::METHOD_POST)->setPostValue('product_id', $product->getId());
64+
$this->dispatch('reports/report_product/view');
65+
5666
/** @var ViewedProductsTabBlock $viewedProductsTabBlock */
5767
$viewedProductsTabBlock = $this->layout->createBlock(ViewedProductsTabBlock::class);
58-
$product = $this->productRepository->getById(1);
59-
$this->eventManager->dispatch('catalog_controller_product_view', ['product' => $product]);
6068

6169
$collection = $viewedProductsTabBlock->getPreparedCollection();
6270

0 commit comments

Comments
 (0)