mixpanel / mixpanel-js

Official Mixpanel JavaScript Client Library
https://mixpanel.com/help/reference/javascript
Other
888 stars 312 forks source link

`$initial_referrer` property shows up as `"$direct"`, even when it isn't #421

Closed hamirmahal closed 6 months ago

hamirmahal commented 8 months ago

Problem

Screencast from 03-17-2024 02:50:46 PM.webm

hamirmahal commented 8 months ago

$initial_referrer and $initial_referring_domain should include "github", no?

hamirmahal commented 6 months ago

@tdumitrescu is this the intended behavior?

chiyc commented 6 months ago

Hi, @hamirmahal. It looks like in this case that the default Referrer-Policy prevents the Referer header from being sent when clicking the link because the browser is requesting an HTTP protocol resource from an HTTPS resource.

You can check the value of document.referrer in the console to confirm that the Referer value is empty. We can't determine why it's empty, so any empty value will be treated as "$direct".

hamirmahal commented 6 months ago

Hmm... I notice this issue even when going from one HTTPS resource, https://github.com/hamirmahal/FilmFinder, to another HTTPS resource, https://hamirs-filmfinder.vercel.app/.

hamirmahal commented 6 months ago

How does Mixpanel recommend tracking the source of page views?

Screencast from 05-16-2024 03:57:20 PM.webm

chiyc commented 6 months ago

Hmm... I notice this issue even when going from one HTTPS resource, https://github.com/hamirmahal/FilmFinder, to another HTTPS resource, https://hamirs-filmfinder.vercel.app/.

You'll see it's common for sites to add a "noreferrer" attribute to external links, which will also prevent the referrer from being provided. Here's the HTML for the link you clicked from GitHub where you can see that's what's happening.

<a title="https://hamirs-filmfinder.vercel.app/" role="link" target="_blank" rel="noopener noreferrer nofollow" class="text-bold" href="https://hamirs-filmfinder.vercel.app/">
  hamirs-filmfinder.vercel.app/
</a>

How does Mixpanel recommend tracking the source of page views?

Besides looking at referrer, the most common way to track incoming traffic sources is through UTM parameters that you add to your links. The Mixpanel SDK will pick those up and automatically add those to the page view events. So if someone visited a link such as https://hamirs-filmfinder.vercel.app/?utm_source=github, then our page view events will track utm_source as an event property.

hamirmahal commented 6 months ago

Ah, that makes sense. Thank you for the help.

hamirmahal commented 5 months ago

When using utm_source to track links to a page, I noticed that utm_source can stick around in the URL in the user's address bar after navigating to a page.

Is there a recommended, or industry-standard way to remove, or clean that from the URL after using utm_source to track where traffic came from?

chiyc commented 5 months ago

I don't know about standards, but many websites just leave them in the URL after a user lands. With Mixpanel tracking, it can be useful to have the UTM parameters recorded on any events that are fired while the user is on the same page.

You could manually remove those query parameters using the browser history API after the customer lands, but how you approach that will depend on the rest of your data needs.