diff --git a/Block/Init.php b/Block/Init.php index 7096d69..b4d831d 100644 --- a/Block/Init.php +++ b/Block/Init.php @@ -84,4 +84,15 @@ public function isCheckoutStartedPage(): bool && $request->getControllerName() === 'index' && $request->getActionName() === 'index'; } + + public function getPageSection(): ?string + { + $request = $this->getRequest(); + + return [ + 'cms/index/index' => 'homepage', + 'catalog/category/view' => 'product_collection', + 'catalog/product/view' => 'product_details', + ][$request->getRouteName() . '/' . $request->getControllerName() . '/' . $request->getActionName()] ?? null; + } } diff --git a/CHANGELOG.md b/CHANGELOG.md index ffe3f4a..0b8ba2c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## 1.0.3 - 2026-11-03 + +- Report the page section (homepage, product collection or product details) from Magento's route, so page views, product views and Webchat messages say where they happened. + ## 1.0.2 - 2026-10-28 - `bin/magento module:uninstall Hellotext_Tracking --remove-data` now removes the Hellotext integration, its OAuth consumer and token, and the module configuration. Before, Magento skipped the module's uninstall script because the module was not registered in `setup_module`. diff --git a/Test/bridge.test.js b/Test/bridge.test.js index a40cda9..bdc6d93 100644 --- a/Test/bridge.test.js +++ b/Test/bridge.test.js @@ -124,6 +124,30 @@ test('identifies an authenticated customer before storefront events and keeps tr assert.deepEqual(calls.filter(([name]) => name === 'track').map(([, action]) => action), ['page.viewed', 'product.viewed']); }); +test('sets the page section on the page each initialize builds, before storefront events', async () => { + const sections = []; + let initialize; + const sdk = { + initialize: async () => { sdk.page = { section: null }; }, + track: async (action) => { sections.push([action, sdk.page.section]); return { failed: false }; } + }; + runBridge({ + window: {}, + document: { + querySelector: () => productElement({ name: 'Shirt', reference: 'magento:store-one:product:42', sku: 'TS-42', source: 'magento' }), + querySelectorAll: () => [], + addEventListener: () => {} + }, + console, Promise, URL, setTimeout, + require: (_deps, callback) => callback(sdk), + define: (_deps, factory) => { initialize = factory(); } + }); + + initialize({ businessId: 'public-business', apiRoot: 'http://api.lvh.me:3200/v1', pageSection: 'product_details' }); + await new Promise(setImmediate); + assert.deepEqual(sections, [['page.viewed', 'product_details'], ['product.viewed', 'product_details']]); +}); + test('product observation URL excludes query parameters and fragments', async () => { const calls = []; let initialize; diff --git a/Test/init.test.php b/Test/init.test.php index 678de99..2943c02 100644 --- a/Test/init.test.php +++ b/Test/init.test.php @@ -34,4 +34,13 @@ public function getValue($path, $scope = null) { return match ($path) { if ($block->hasSafeConfig() !== $expected) { throw new \RuntimeException('Checkout route guard failed.'); } } echo "Storefront checkout-route guards passed.\n"; + foreach ([['cms', 'index', 'index', 'homepage'], ['catalog', 'category', 'view', 'product_collection'], + ['catalog', 'product', 'view', 'product_details'], ['cms', 'page', 'view', null], ['catalogsearch', 'result', 'index', null]] + as [$route, $controller, $action, $expected]) { + $context->route = $route; + $context->controller = $controller; + $context->action = $action; + if ($block->getPageSection() !== $expected) { throw new \RuntimeException('Page section mapping failed.'); } + } + echo "Storefront page sections passed.\n"; } diff --git a/composer.json b/composer.json index 8210963..4441a10 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "hellotext/magento", "description": "Connect Magento Open Source stores to Hellotext: storefront activity, customer identity, orders and catalog/customer import.", "type": "magento2-module", - "version": "1.0.2", + "version": "1.0.3", "license": "proprietary", "keywords": [ "magento", diff --git a/etc/module.xml b/etc/module.xml index fed3819..597c443 100644 --- a/etc/module.xml +++ b/etc/module.xml @@ -3,7 +3,7 @@ xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> - + diff --git a/view/frontend/templates/init.phtml b/view/frontend/templates/init.phtml index 9e8c0ff..f2837e3 100644 --- a/view/frontend/templates/init.phtml +++ b/view/frontend/templates/init.phtml @@ -14,6 +14,7 @@ $config = json_encode( 'storeId' => $block->getTrackingStoreId(), 'currency' => $block->getTrackingCurrency(), 'checkoutStarted' => $block->isCheckoutStartedPage(), + 'pageSection' => $block->getPageSection(), ]], JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT ); diff --git a/view/frontend/web/js/bridge.js b/view/frontend/web/js/bridge.js index aca8915..d0266af 100644 --- a/view/frontend/web/js/bridge.js +++ b/view/frontend/web/js/bridge.js @@ -76,7 +76,12 @@ define(['Magento_Customer/js/customer-data', 'Hellotext_Tracking/js/cart-tracker apiRoot: config.apiRoot, // Web push needs a service worker served from the store's own origin. push: false - })).then(function () { session = Hellotext.session; }); + })).then(function () { + session = Hellotext.session; + // Initializing builds a new page, so set its section again: every event of this page reports it, + // Webchat messages included. + if (Hellotext.page) { Hellotext.page.section = config.pageSection; } + }); } function synchronizeSession(Hellotext) {