Send identity from your server
Some events can only be confirmed on your backend: email verification, SSO sign-up, or payment.
If your server sends these events to analytics, include your user ID. Roadway links the user to earlier visits through browser page views after the user is identified in the browser, so identify the user in the browser too. Passing the browser's anonymous ID still helps your analytics tool's own reports.
The pattern
- Read the browser's anonymous ID.
- Send it to your server with the sign-up request.
- Include it, with your user ID, when the server sends the event.
Step 1: read the anonymous ID in the browser
// GA4
gtag('get', 'G-XXXXXXXXXX', 'client_id', function (clientId) {
document.getElementById('ga_client_id').value = clientId; // include in your sign-up request
});
// Segment
var segmentAnonymousId = analytics.user().anonymousId();
// PostHog
var posthogDistinctId = posthog.get_distinct_id();
var posthogDeviceId = posthog.get_property('$device_id');
The GA4 callback is asynchronous. Wait for it before you submit the form.
Step 2: send events from your server
GA4 Measurement Protocol
Send a sign_up event with the visitor's client_id and your user_id:
curl -X POST "https://www.google-analytics.com/mp/collect?measurement_id=G-XXXXXXXXXX&api_secret=YOUR_API_SECRET" \
-H "Content-Type: application/json" \
-d '{
"client_id": "1234567890.1700000000",
"user_id": "usr_8f2c",
"events": [{ "name": "sign_up", "params": { "method": "email" } }]
}'
Create the API secret in GA4 under Admin > Data streams > your stream > Measurement Protocol API secrets. Keep it on the server.
Segment
analytics.identify({
userId: 'usr_8f2c',
anonymousId: anonymousIdFromBrowser,
traits: { plan: 'trial' }
});
PostHog
posthog.capture({
distinctId: 'usr_8f2c',
event: '$identify',
properties: { $anon_distinct_id: anonymousDistinctIdFromBrowser }
});
Confirm the exact server-side identify call for your PostHog SDK in PostHog's docs. SDKs differ.
Verify
- Complete a test sign-up.
- Confirm browser page views after sign-up carry the user ID.
Troubleshooting
| Problem | Cause | Fix |
|---|---|---|
| Users never linked to visits | Browser page views after sign-up don't carry the user ID | Identify the user in the browser as well as on the server |
| Server event shows as a new visitor in your analytics tool | Missing client_id, anonymousId, or $anon_distinct_id |
Pass the browser ID |
| Measurement Protocol events don't appear in reports | Missing or wrong api_secret, or invalid payload |
Validate with the Measurement Protocol validation server |
Vendor docs: GA4 Measurement Protocol, Segment: Node.js source, PostHog: identifying users