Skip to content

Analytics Provider is not working when I try to use Gtag #2426

@lachlan-connell-aligent

Description

Describe the bug
The new changes in the canary branch for the analytics provider give this error:

Image

To Reproduce
Steps to reproduce the behavior:

  1. Setup new analytics provider and connect to GA4
  2. Go to a PDP Page
  3. See error

Expected behavior
It should not show this error in the console and cause the app to have a server error.

Screenshots
I believe the static function that was previously in bodl, waitForBodlEvents solved this error.

I implemented something similar in my code called: waitForGtag:

  static waitForGtag(callback: () => void, iteration = 0) {
    if (typeof window !== 'undefined' && typeof window.gtag === 'function') {
      callback();

      return;
    }

    if (iteration >= 10) {
      console.warn('gtag not initialized after waiting');

      return;
    }

    setTimeout(() => {
      GoogleAnalyticsProvider.waitForGtag(callback, iteration + 1);
    }, 300);

I then just wrapped all my events in GoogleAnalyitcsProvider with this like so:

 private getCartEvents() {
    return {
      cartViewed: (payload, metadata) => {
        GoogleAnalyticsProvider.waitForGtag(() => {
          gtag('event', 'view_cart', {
            event_id: metadata.eventUuid,
            channel_id: metadata.channelId,
            currency: payload.currency,
            value: payload.value,
            items: payload.items.map((item) => {
              return {
                item_name: item.name,
                item_id: item.sku ?? item.id,
                price: item.price,
                quantity: item.quantity,
                currency: payload.currency,
                item_brand: item.brand,
                variant_id: item.variant_id,
                item_category: item.categories?.at(0),
                item_category2: item.categories?.at(1),
                item_category3: item.categories?.at(2),
                item_category4: item.categories?.at(3),
                item_category5: item.categories?.at(4),
              };
            }),
          });
        });
      },

etc ....

Additional context
@chanceaclark have been talking to you about analytics provider previously so just tagging you on this one :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions