Issue with Visit ID when tracking events via Piwik.Tracker in .NET desktop app

Hi everyone,

I’m tracking events from a .NET desktop application (C#) to Matomo using the Piwik.Tracker NuGet package (version 3.0.0).

Events are first stored locally, grouped by unique SessionId, then sent later to Matomo (respecting chronological order within the session group). I use custom dimensions to identify session.
Here is the relevant tracking code:

PiwikTracker.SetMatomoVisitTime(Timestamp);
PiwikTracker.SetUserId(UserId);
PiwikTracker.SetCustomTrackingParameter(“dimension1”, MachineId);
PiwikTracker.SetCustomTrackingParameter(“dimension2”, SessionId);
State = PiwikTracker.DoTrackEvent(category, action, name, value);
? StateType.Synced
: StateType.NotSynced;

Additionally, before sending the first event of each session, I call:

PiwikTracker.SetForceVisitDateTime()

to force the creation of a new visit (i.e., increment the visit ID). The reason for doing this is separating events with the same SessionId in separate visits.

Problem

I’m observing that after forcing a new visit ID for a specific event, some events sent afterward are still associated with the previous visit ID. This seems inconsistent, especially since I explicitly forced a new visit.

I would expect that, once I force a new visit ID, all subsequent tracked events should belong to that new visit. This should happen regardless of the timestamps associated with those events.

Question

  • Why are some events still linked to the previous visit ID even after forcing a new one?
  • Is this related to how Matomo processes timestamps when using SetMatomoVisitTime()?
  • What is the correct way to ensure that all events after a forced visit increment are assigned to the new visit?

Additional Context

Events may have timestamps in the past (since they are queued locally). I suspect Matomo might be re-grouping events into visits based on timestamp rather than call order.

Goal

I need a reliable way to ensure that events are grouped strictly according to when I force a new visit.

Thank you in advance for your support. Any clarification on how visit attribution works in this scenario would be greatly appreciated.

It might be because you aren’t resetting the idJavaScript Tracking Client: Integrate - Matomo Analytics (formerly Piwik Analytics) - Developer Docs - v5

Hi Jason, thanks for the suggestion. I am using the C# client implementation of the Tracking API and I cannot find the equivalent method for resetting the id. I’m probably missing something.