The module causes a lot of extra requests to its events endpoint

Created on 26 March 2020, over 4 years ago
Updated 29 March 2023, over 1 year ago

Problem/Motivation

We've used the module on a large newspaper site that also has a shop that is only visible on a tiny section of the page. However, it currently adds its JS library to every page unconditionally and checks if there are any events to report on.

I assume it works like that to not break page cache?

Also, I'm not sure if the product view tracking even works for anonymous users. It adds it to the same temp store, but the product page is then likely cached and only the first anon user is tracked? The other events work as users have a session once they put something in their cart.

Proposed resolution

The product view event is not personalized, IMHO that should be added to drupalSettings on viewing a product (so he kernel response change instead of entity_view is a bit problematic).

The cart and checkout are trickier, in a somewhat similar case I used a pre_render callback in hook_page_attachments() as that is basically lazy-buildered with dynamic page cache, but I did print that into a html_tag as inline JS. If the goal is to avoid that, then that's a bit challenging, but sharing that approach anyway:

  $html_tag = [
    '#type' => 'html_tag',
    '#tag' => 'script',
  ];
  $html_tag += \Drupal::service('plugin.manager.element_info')->getInfo($html_tag['#type']);
  array_unshift($html_tag['#pre_render'], 'yourmodule.callback_service:dataLayerBuilder');

  $attachments['#attached']['html_head'][] = [
    $html_tag,
    'factory_piano_tp',
  ];

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

🐛 Bug report
Status

Active

Version

1.0

Component

Miscellaneous

Created by

🇨🇭Switzerland berdir Switzerland

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇳🇴Norway zaporylie

    Also, I'm not sure if the product view tracking even works for anonymous users. It adds it to the same temp store, but the product page is then likely cached and only the first anon user is tracked? The other events work as users have a session once they put something in their cart.

    8.x-2.x here. I am seeing the page view tracking to be completely uncacheable causing every page load to be slowed down because CommerceEventsSubscriber::trackProductView needs to load entities and do all calculations, data manipulations etc for every single request. The data used for the event should be read from cache if the page is served from cache - there is no point in skipping the cache completely. To give some numbers - we were able to reduce page load 2x by caching only the price calculations - to be fair, that was a site with quite complex price calculation chain.

Production build 0.71.5 2024