6

I am working on a magento extension used for analytics purposes which requires functionality which responds to a variety of Magento events.

One of these events is catalog_controller_product_view which I need to extra the product data and do something with (I have no problems with this bit).

The issue I have is after the first time viewing a product Magento no longer raises the catalog_controller_product_view event for my Observer execute method is never called.

I have logged every Magento event as a debugging measure and the catalog_controller_product_view is definitely not being raised on every product view only the first one per product. In addition I can't see any event to hook into, Ive tried injecting the Request Interface and getting the request response which may contain product data but no luck.

Any ideas how to solve this?

Event.xml

<event name='catalog_controller_product_view'>
        <observer
                name='Path/To/Observer/ViewProduct'
                instance='Path/To/Observer/ViewProduct'
        />
</event>

Existing Observer (Path/To/Observer/ViewProduct) relevant lines:

$event = $observer->getEvent();
$product = $event->getData('product');
// do something with $product

To clarify what I'm after I need a way to accurate retrieve a product view regardless of caching, or a way to add the event to cache exceptions (if possible).

  • Just for a test, try disabling FPC cache and see if it works. – Phoenix128_RiccardoT May 22 '16 at 20:12
  • @RiccardoT If i disable that it does work. I would have thought the view product event would be raised on every product view though. Dsiabling the cache isnt something customers who will be using this will do! – Chris Edwards May 22 '16 at 20:17
  • I know, it was just for a test. IMHO you cannot handle that scenario with an event and FPC enabled. I think the best way you have is to handle it with an Ajax call. – Phoenix128_RiccardoT May 22 '16 at 20:34
  • is there any reference materials you can provide for me to take a look at I'm not sure how to do that. Can I Still access the same data? – Chris Edwards May 22 '16 at 20:35
  • Unfortunately I cannot give you any material on how to do it. BAsically you just have to create a controller reading your product id and raising the tracking code you need. – Phoenix128_RiccardoT May 22 '16 at 20:37
2

catalog_controller_product_view event cannot be used in conjunction with FPC cache since Magento will not raise it after the first visit.

Option 1 - The AJAX way:

You have to:

  • Create a controller accepting your product's id
  • Add a JS block to your product's page calling your controller and passing the product's id
  • Insert your tracking code in your new controller

Option 2 - Predispatch event (not tested):

Otherwise you could try using controller_action_predispatch event that should be called even with FPC enabled.

You will have to get the product's information by your own in this case.

  • 1
    Even controller_action_predispatch is not called in conjuction with FPC. It is actually not called for already visited pages but is called for new pages. – Arvind07 Jul 19 '16 at 10:48
7

controller_action_predispatch is cached when FPC is on. So you cannot rely on it.

You need to catch controller_front_send_response_before. It is fired even when FPC is on.

You /etc/frontend/events.xml should look like:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="controller_front_send_response_before">
        <observer name="ws_ag_controller_front_send_response_before" instance="Namespace\Module\Observer\ResponseBefore" />
    </event>
</config>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.