nystudio107 / craft-instantanalytics

DEPRECATED: Instant Analytics brings full Google Analytics support to your Twig templates and automatic Craft Commerce integration with Google Enhanced Ecommerce.
https://nystudio107.com/
Other
20 stars 9 forks source link

Parent Category disappeared from GA, attributing all events now to "not set" category #58

Closed heymynameisdina closed 1 year ago

heymynameisdina commented 3 years ago

Question

I've read through documentation, as well as other user issues and PRs and am not able to find the answer if you could please help. As of June 15, our previously properly working IA events started attributing all events to the category "not set".

Previous events (with accurate data):

Screen Shot 2021-08-03 at 7 05 25 PM

Current events (with inaccurate data):

Screen Shot 2021-08-03 at 7 05 36 PM

At some point, the Parent Category dropped off of our category label here. You'll see in the accurate events, each event has the Parent Category with a pipe prior to our custom category labeling. I believe that is being done on your end here. For some reason that is now missing and all of our events live in the "not set" category.

Additional context

Instant Analytics 1.1.12 Craft CMS 3.6.16 Craft Commerce Pro 3.4.0

We recently upgraded from Craft 2 -> 3 if that is relevant!

khalwat commented 3 years ago

I'm guessing the upgrade from Craft 2 to Craft 3 is very relevant, as it'd result in a large number of changes to the website.

So the initial screenshot is data coming from a Craft 2 site running a Craft 2 version of IA, and the second screenshot is from Craft 3 site running the Craft 3 version of IA?

heymynameisdina commented 3 years ago

Thanks for the quick response, Andrew. Apologies here, I had our versions wrong. On June 23rd, we updated the following:

Craft: 3.5 to 3.6 Commerce 3.2 to 3.3

Our GA events transitioned to that second screenshot on June 16, however.

The only other thing I'm seeing remotely related to IA on our end -> We had made some recent adjustments to how we generate those category breadcrumbs (Audio/Headphones & Earbud Cases), but I've validated that the expected values are the ones that are being sent. Meaning we populate and send Audio/Headphones & Earbud Cases and that's exactly what's showing up in GA. While we're building them differently than before, we're sending the same exact information we did prior to June 16.

The only difference I'm seeing is the missing parent category and pipe prefixed in GA...can you confirm that the parent category and pipe are generated on the IA side? I'm not seeing anywhere in our codebase that is now or was previously producing that prefix. Thank you again for your help!

heymynameisdina commented 3 years ago

hey @khalwat can you confirm that the Top Level Category and Pipe prefix are automatically applied by Instant Analytics? That seems to be the missing piece here...

khalwat commented 3 years ago

I'm not sure how this could change, but Instant Analytics does have a Product Category that you can set either in the config.php or in the plugin settings, which is where you choose the field in the Commerce Product that you use for the category.

Can you check on that?

hahnzilla commented 3 years ago

One related issue (can open post this elsewhere if you prefer) here is that it looks like if you select a single category in a hierarchical structure it formats it with a pipe and slash. For example:

Screen Shot 2021-08-30 at 3 28 02 PM

is formatted as Audio|Audio/Headphones.

Only reason I brought it up here is that I think Audio| shouldn't be present. Believe that's going to throw off the hierarchy in analytics:

The category to which the product belongs (e.g. Apparel). Use / as a delimiter to specify up to 5-levels of hierarchy (e.g. Apparel/Men/T-Shirts).


But for what it's worth, I did validate that Instant Analytics is passing a category through

hahnzilla commented 3 years ago

alright hey I believe I figured out the issue with instant analytics not setting the category. In our codebase, we're eagerloading the category on a purchasable's products in an event listener for later on in the lifecycle. It looks like instant analytics is specifically looking for a CategoryQuery in pullDataFromField.

You can replicate the issue by adding this event listener and then trying to add an item to the cart:

        Event::on(
            LineItemsService::class,
            LineItemsService::EVENT_POPULATE_LINE_ITEM,
            function ($event) {
                $product = $event->sender->purchasable->product;

                Craft::$app->getElements()->eagerLoadElements(
                    Product::class,
                    [$product],
                    [
                        'mainProductType' // category field
                    ]
                );
            }
        );

this isn't exactly how we're doing it, but seemed like the easiest way to show the issue

khalwat commented 1 year ago

Yep, it's this that causes it to exit early:

            // If the source field isn't an object, return nothing
            if (!is_object($srcField)) {
                return $result;
            }

ref: https://github.com/nystudio107/craft-instantanalytics/blob/9e726336600622b504f4de3d3ce211554b84c897/src/services/Commerce.php#L473

Probably it should handle the case where it is an array, due to eager loading.

khalwat commented 1 year ago

Addressed in: https://github.com/nystudio107/craft-instantanalytics/commit/b8b25c85225ded4c1c046c64fd2c7132f390c837 & https://github.com/nystudio107/craft-instantanalytics/commit/92a6fb29bd5db7cdc238191251746bc8bb7ceac8

Craft CMS 3:

You can try it now by setting your semver in your composer.json to look like this:

    "nystudio107/craft-instantanalytics": "dev-develop as 1.1.18”,

Then do a composer clear-cache && composer update

…..

Craft CMS 4:

You can try it now by setting your semver in your composer.json to look like this:

    "nystudio107/craft-instantanalytics": "dev-develop-v4 as 4.0.2",

Then do a composer clear-cache && composer update