segment-integrations / analytics.js-integration-google-analytics

The Google Analytics analytics.js integration.
https://segment.com/docs/integrations/google-analytics/
MIT License
20 stars 23 forks source link

SPA sessions with referrer are broken #12

Closed MicheleBertoli closed 8 years ago

MicheleBertoli commented 9 years ago

The problem

At YPlan, we found that using Segment on a Single Page Application is causing Google Analytics to break the sessions, giving us the wrong PPC vs Organic attributions. The cause is that Segment is always sending the original referrer with the current (updated) url.

How to reproduce it

We created a simple SPA with React and React Router to reproduce the problem.

Landing on the application and clicking the links (i.e. updating the url) from a different referrer (e.g. jsfiddle) with the utm parameters results in a split session:

screen shot 2015-11-27 at 07 54 13

This is the "raw" Google Analytics data sent via Segment:

v:1
_v:j40
a:120322824
t:pageview
_s:1
dl:http://localhost:8000/?utm_source=michele&utm_medium=spa/
dr:http://fiddle.jshell.net/_display/
dp:/?utm_source=michele&utm_medium=spa/
ul:en-us
de:windows-1252
dt:Segment SPA
sd:24-bit
sr:1680x1050
vp:1123x332
je:1
fl:19.0 r0
_u:SCCAgAAj~
jid:
cid:1913804964.1439278034
tid:UA-32643507-4
v:1
_v:j40
a:120322824
t:pageview
_s:2
dl:http://localhost:8000/two
dr:http://fiddle.jshell.net/_display/
dp:/two
ul:en-us
de:windows-1252
dt:Segment SPA
sd:24-bit
sr:1680x1050
vp:1123x332
je:1
fl:19.0 r0
_u:SCCAgAAj~
jid:
cid:1913804964.1439278034
tid:UA-32643507-4
z:1688857593
v:1
_v:j40
a:120322824
t:pageview
_s:3
dl:http://localhost:8000/three
dr:http://fiddle.jshell.net/_display/
dp:/three
ul:en-us
de:windows-1252
dt:Segment SPA
sd:24-bit
sr:1680x1050
vp:1123x332
je:1
fl:19.0 r0
_u:SCCAgAAj~
jid:
cid:1913804964.1439278034
tid:UA-32643507-4
z:1714268981

Overriding the referrer

We read the documentation and we tried to override the referrer, as follows:

history.listen(() => {
  analytics.page('category', 'name', {
    referrer: 'https://www.itsarainysunnyday.com'
  });
});

But we found that Segment keeps on sending the original referrer to Google Analytics:

v:1
_v:j40d
a:2007255171
t:pageview
_s:1
dl:http://localhost:8080/
dr:http://fiddle.jshell.net/_display/
dp:/
ul:en-us
de:windows-1252
dt:category name
sd:24-bit
sr:1680x1050
vp:1676x216
je:1
fl:19.0 r0
_u:SCCAgAAj~
jid:
cid:2131063951.1440664624
tid:UA-32643507-4
z:1217894582

While it's sending the right one in the context:

path: "/"
referrer: "https://www.itsarainysunnyday.com"
search: ""
title: "Segment SPA"
url: "http://localhost:8080/"

Any suggestions?

hankim813 commented 9 years ago

Hey @MicheleBertoli,

Segment actually doesn't send GA the referrer data -- that's parsed and calculated automatically by the GA's own JS library they load under the hood.

You can see Segment's integration mapper code for how they process any .page() calls and they never actually define the dr or the document referrer anywhere:

https://github.com/segment-integrations/analytics.js-integration-google-analytics/blob/master/lib/index.js#L161-L207

Also in GA's docs for SPA's, they explicitly ask you not to override the referrer anyway:

https://developers.google.com/analytics/devguides/collection/analyticsjs/single-page-applications

So I'm not so sure if it is the referrer that is causing the split sessions :/

hankim813 commented 9 years ago

I think this might be the cause:

GA docs say that updating the location for SPA's may cause split sessions... and I can confirm that Segment is parsing the url property of your .page() calls and overriding the location:

https://github.com/segment-integrations/analytics.js-integration-google-analytics/blob/master/lib/index.js#L178

MicheleBertoli commented 9 years ago

Thank you very much for looking into our issue @hankim813.

I can confirm that (as we wrote in the first comment):

The cause is that Segment is always sending the original referrer with the current (updated) url.

Therefore, you are right when you say:

So I'm not so sure if it is the referrer that is causing the split sessions :/

The cause it's not the referrer itself, it's a combination between referrer and url. The problem of course exists only when the referrer is present.

Just to clarify:

Any plans to fix the issue?

hankim813 commented 9 years ago

@MicheleBertoli so just to be on the same page are you saying that the problem only exists when the referrer is present when we are also setting the location as the url?

MicheleBertoli commented 9 years ago

Hello @hankim813,

it looks like Segment is always setting the location, even when the url property is not present.

Because of that, when the referrer exists, you can see a single session being attributed to two different sources, like this:

8c8b3d8e-9874-11e5-8d59-c464aacdd1e1
hankim813 commented 9 years ago

Ahh so the location is actually being set for GA here:

https://github.com/segment-integrations/analytics.js-integration-google-analytics/blob/master/lib/index.js#L178

Hmmm the url property should always be there when you use our .page() call. That is automatically populated by our API. So if this issue is exclusive to SPAs, then perhaps a propose solution could be an optional setting that you can check in the GA settings that tells us if you have an SPA, and we can wrap that line 178 with a conditional logic that doesn't set the location?

What do you think. Do you know if not setting that field at all will have any other negative effects for the data?

MicheleBertoli commented 9 years ago

Thank you very much @hankim813,

the flag in the Google Analytics integration's settings sounds good.

The Google's documentation clearly says "do not update the location", therefore we think that it wont have any negative effects.

sperand-io commented 8 years ago

fixed in #14