Skip to content
This repository has been archived by the owner on Aug 7, 2024. It is now read-only.

Instant analytics not sending purchase data #49

Open
jripmeester opened this issue Mar 29, 2021 · 9 comments
Open

Instant analytics not sending purchase data #49

jripmeester opened this issue Mar 29, 2021 · 9 comments
Labels

Comments

@jripmeester
Copy link

Question

We've updated a client webshop to commerce 3 and did a full refactor of the front-end.
After updating the website and launching the website all instant analytics data is send nicely except for the purchase event. The store had multiple orders last weekend but none we're received in analytics but add to cart events are registered.

What's the best wat to check what events are being send? Logging shows no errors.
Is there a way to test instant analytics without me var_dump-ing in your code files?

Additional context

Craft CMS - 3.6.11.1
Craft Commerce - 3.2.17.3
Instant Analytics - 1.1.11

@khalwat
Copy link
Contributor

khalwat commented Mar 29, 2021

It should be logging any time events are sent -- are you not seeing anything?

Did you implement any kind of caching on the frontend that could affect things?

@jripmeester
Copy link
Author

jripmeester commented Mar 29, 2021

Are the events also logged in production? Because in that case i don't see them in web.log.
We had a service-worker in place but we removed that as it did lead to some weird situations.

No other caching in place right now.

@jripmeester
Copy link
Author

@khalwat After some strategic dumping of vars in our staging env i found out that the order event is triggered but the event is not received in Analytics. So or it's blocked in analytics somehow or the
$analytics->setProductActionToPurchase();
$analytics->sendEvent();
on line 58 of services/commerce.php are not doing their job.

But this seems on GA side. Any idea how to further debug this?

@jripmeester
Copy link
Author

@khalwat Sorry to keep buggin you with this. But is there a way to trigger the order send event from twig. To test if a manual trigger does work or maybe give an error.

@khalwat
Copy link
Contributor

khalwat commented Mar 30, 2021

What version of GA are you running?

@jripmeester
Copy link
Author

jripmeester commented Mar 30, 2021 via email

@jripmeester
Copy link
Author

@khalwat Some more debugging led me too trying trigger the orderComplete function from a custom module variable. When i use that path all is working fine. So it seems that the webhook way used by the payment service is the killer here.

Does that classify as a bug?

@jripmeester
Copy link
Author

jripmeester commented Apr 17, 2021

@khalwat Last week we went a bit deeper with pin pointing this error and we found something out.

The orders of logged in users is getting sent to GA, orders of guests are not.
This webshop is behind cloudflare. Bypassing Cloudflare made al orders come through.

Difference between the 2 situations is that:

  1. a guest with Cloudflare gets a _ga clientId in the format: 1197138454.1617354777

  2. An logged in user with Cloudflare or all users/guests without Cloudflare get the _ia clientId in the format: 75da40e3-95ea-4d34-bcbe-57dfbc8ca085

Situation 1 is the majority of the visitors.

We bypassed this now to (ugly fix), change:

    private function gaParseCookie(): string
    {
        $cid = '';
        if (isset($_COOKIE['_ga'])) {
            $parts = preg_split('[\.]', $_COOKIE['_ga'], 4);
            if ($parts !== false) {
                $cid = implode('.', \array_slice($parts, 2));
            }
        } elseif (isset($_COOKIE['_ia']) && $_COOKIE['_ia'] !== '') {
            $cid = $_COOKIE['_ia'];
        } else {
            // Only generate our own unique clientId if `requireGaCookieClientId` isn't true
            if (!InstantAnalytics::$settings->requireGaCookieClientId)  {
                $cid = $this->gaGenUUID();
            }
        }
        if (InstantAnalytics::$settings->createGclidCookie && !empty($cid)) {
            setcookie('_ia', $cid, strtotime('+2 years'), '/'); // Two years
        }

        return $cid;
    }
private function gaParseCookie(): string
    {
        $cid = '';
        if (isset($_COOKIE['_ia']) && $_COOKIE['_ia'] !== '') {
            $cid = $_COOKIE['_ia'];
        } else {
            // Only generate our own unique clientId if `requireGaCookieClientId` isn't true
            if (!InstantAnalytics::$settings->requireGaCookieClientId)  {
                $cid = $this->gaGenUUID();
            }
        }
        if (InstantAnalytics::$settings->createGclidCookie && !empty($cid)) {
            setcookie('_ia', $cid, strtotime('+2 years'), '/'); // Two years
        }

        return $cid;
    }

This makes sure every visitor gets the_ia cookie. But this is far from a fix.
Does this info give you some more insights on what a good fix might be?

@khalwat
Copy link
Contributor

khalwat commented Apr 17, 2021

mmmm so it sounds like it's something specific to CloudFlare in terms of how this is not working for you.

It looks like your change is just dropping any parsing of the _ga cookie, which causes it to fall back on using the _ia cookie... but that means we're not retaining any unique id from Google, which probably has some unfortunate implications in terms of tracking.

I guess I'm unclear why if the _ga cookie looks like this: 1197138454.1617354777 that the original code doesn't work. Wouldn't that set the $cid to properly?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants