analytics icon indicating copy to clipboard operation
analytics copied to clipboard

Two problems with using Mixpanel

Open TimToshiro opened this issue 2 years ago • 5 comments

Hello. I have two problems related to work with Mixpanel.

The first one: When I use function call at page opening - then event comes out incorrectly and has extremely inconvenient name among events on Mixpanel site itself. изображение

const analytics = Analytics({ app: 'Test', plugins: [ mixpanelPlugin({ token: 'test', }), googleAnalytics({ measurementIds: ['G-TEST'], }) ] })

analytics.page({ url: pageUrl, });

Second: When I add information to a user, I see the name as a built-in parameter, but the email address is passed as custom and initially I don't see it in the table in the list of users. But I can see it inside properties of the user if I open full information. I don't have this problem with the name. изображение

analytics.identify('testID', { email: email, name: name, })

TimToshiro avatar Mar 22 '23 20:03 TimToshiro

Hello there. I'm not using mixpanel myself. Double check the network calls in the browser dev tools to ensure the values being sent are defined and not null

image

DavidWells avatar May 27 '23 21:05 DavidWells

I have the same problem about wrong naming of the "page_view" event. By activating the debug in mixpanel plugin I can clearly see that the page url is sent as event_name (instead of "page_view").

ousmaneNdiaye avatar Jul 31 '23 08:07 ousmaneNdiaye

@TimToshiro I think your second problem comes from the missing $ prefix for some reserved properties...

A temporary fix could be :

analytics.page({
url: pageUrl,
$email: "[email protected]"
});

You can find the list of mixpanel reserved properties here : https://docs.mixpanel.com/docs/tracking/how-tos/user-profiles#reserved-user-properties

ousmaneNdiaye avatar Jul 31 '23 08:07 ousmaneNdiaye

I noticed the same problem with page views on mixpanel as well.

Sounds like the email property has to be changed to $email and that transformation should happen at the plugin

I also noticed some related PRs that are old but haven't been pulled. Is this library still under active maintance?

https://github.com/DavidWells/analytics/pull/115 https://github.com/DavidWells/analytics/pull/116

fotoflo avatar Mar 22 '24 05:03 fotoflo

Update - solved.

I took a look at the mixpanel plugin and there's a way to override the page event: https://github.com/DavidWells/analytics/blob/master/packages/analytics-plugin-mixpanel/src/browser.js

So this is how i configured my analytics.tsx

import type { ReactNode } from "react";
import React, { useEffect } from "react";
import Analytics from "analytics";
import { AnalyticsProvider } from "use-analytics";
import mixpanelPlugin from "@analytics/mixpanel";
import { useRouter } from "next/router";
import { env } from "src/env/client.mjs"; // Make sure this module is typed

const analyticsInstance = Analytics({
  app: "fastmonitor",
  plugins: [
    mixpanelPlugin({
      token: env.NEXT_PUBLIC_MIXPANEL_PROJECT_TOKEN as string, // Type casting as string, assuming it's always defined
      track_pageview: true,
      pageEvent: "page_view",
    }),
  ],
  debug: env.NEXT_PUBLIC_HOST.includes("localhost"),
});

export default function AnalyticsHOC({ children }: { children: ReactNode }) {
  const { pathname, query, asPath } = useRouter();

  useEffect(() => {
    analyticsInstance.page({
      path: pathname,
      query: query as { [key: string]: string },
      host: window.location.origin,
    });
  }, [pathname, query, asPath]);

  return (
    <AnalyticsProvider instance={analyticsInstance}>
      {children}
    </AnalyticsProvider>
  );
}

And this is how it's rendering in mixpanel:

image

and for reference, in my _app.tsx (im using pages router)

  return (
    <>
        <SessionProvider session={session}>
            <AnalyticsHOC>
              <Component {...pageProps} />
            </AnalyticsHOC>
        </SessionProvider>
    </>
  );
};

fotoflo avatar Mar 22 '24 06:03 fotoflo