How can I accurately track conversions with Google Analytics 4 (api)?

84 views Asked by At

I run a small webapp. The app is hosted on app.mydomain.com and is custom built with React. My 'main' website is just on mydomain.com. This is a Wordpress website that mainly drives the traffic with a blog.

I installed the same Google Tag Manager tracking code on both domains and linked a GA4 tag. So both the main website and subdomain use the same tracking code. I also tried with the NPM package react-ga4.

Whenever a customer completes a payment, I send a request to Google Analytics through their api. It is worth noting that this happens on the server.

generateGA4Conversion = async (customerId) => {
  const measurement_id = process.env.MEASUREMENT_ID;
  const api_secret = process.env.GA4_API_SECRET;

  fetch(
    `https://www.google-analytics.com/mp/collect?measurement_id=${measurement_id}&api_secret=${api_secret}`,
    {
      method: "POST",
      headers: {
        "User-Agent":
          "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36",
        "Content-Type": "application/json",
      },
      body: JSON.stringify({
        client_id: customerId,
        events: [
          {
            name: "conversion",
            params: { succes: true },
          },
        ],
      }),
    }
  );
};

The problem now is, is that all my conversions are being marked as direct. This cannot be true. I can also see that a lot of sessions are being marked as 'direct'. I guess those are people clicking from the main website (blog) to the app. Basically all my traffic comes from Google Search and Paid search, so I know this cannot be true. I would expect to see al conversions either being marked with the Organic or Paid channel. How can I accurately track conversions with the correct channel?

0

There are 0 answers