nystudio107 / craft-instantanalytics-ga4

Instant Analytics brings full Google GA4 server-side analytics support to your Twig templates and automatic Craft Commerce integration
Other
3 stars 4 forks source link

Using Instant Analytics with blitz caching #14

Closed tomjrjones closed 1 year ago

tomjrjones commented 1 year ago

Hi @khalwat ,

Just wondering if this plugin works with blitz caching? Or will I have to manually add the events within my page templates.

Many Thanks, Tom

khalwat commented 1 year ago

Nope. IA is server-side analytics, which means Craft needs to execute on each request. Blitz uses static caching, which bypasses Craft and just serves up the pre-rendered HTML.

lenvanessen commented 6 months ago

@khalwat i believe there are possibilities as long as you don't have nginx or apache render the HTML Blitz puts out. Blitz has a event that is triggered before it renders the HTML, which we've used to send the collected events:

      // Send the collected events
        Event::on(
            CacheRequestService::class,
            CacheRequestService::EVENT_AFTER_GET_RESPONSE,
            function (ResponseEvent $e) {
                InstantAnalytics::$plugin->ga4->addPageViewEvent();
                InstantAnalytics::$plugin->ga4->getAnalytics()->sendCollectedEvents();
            }
        );

We've gotten many events, especially for commerce, to work this way. It would be nice to have some support in the plug-in.

khalwat commented 6 months ago

Right, I'm assuming people using Blitz will be using the Nginx / Apache rewrites, because otherwise they lose most of the TTFB benefits of using a static cache.