snowplow / snowplow-javascript-tracker

Snowplow event tracker for client-side and server-side JavaScript. Add analytics to your websites, web apps and servers.
http://snowplowanalytics.com
BSD 3-Clause "New" or "Revised" License
543 stars 220 forks source link

Add feature to start new session on campaign change #1055

Open denisov-vlad opened 2 years ago

denisov-vlad commented 2 years ago

Is your feature request related to a problem? Please describe. According to GA documentation it starts new session if referrer isn't equal URL domain or URL has UTM-params. It would be helpful to have the same logic inside Snowplow.

Describe the solution you'd like

Thanks!

paulboocock commented 2 years ago

I don't think it's particularly well documented but the first bullet point already exists, you can call the newSession method to start a new session.

https://github.com/snowplow/snowplow-javascript-tracker/blob/master/trackers/browser-tracker/docs/markdown/browser-tracker.browsertracker.newsession.md

Or window.snowplow('newSession') if using the JavaScript Tracker rather than Browser Tracker.

I'll leave this issue open for the option to reset on UTM param change and/or new referer. That would perhaps be a breaking change though, or need to be disabled by default.

denisov-vlad commented 2 years ago

@paulboocock Thanks! That's exactly what I need.

But there is one issue with newSession usage from code when session timeout happens:

  1. Visit page (vid = 24)
  2. Wait for session timeout
  3. Call newSession before trackPageView (vid = 26)
paulboocock commented 2 years ago

If you wait for the session to timeout and then visit the page, that's going to generate vid 25 immediately because that's a new session. Track Page View isn't when the session begins, when you arrive on the page and initialise the tracker is when the session starts.

In reality there are usually lots of edge cases when vid doesn't increase by only 1, it should be used to sort sessions in order, not to get a count of them (you should count by distinct domain_sessionid for that).