-
Notifications
You must be signed in to change notification settings - Fork 5
Conversation
@yakkomajuri this is the sort of exception that would be really helpful in our docs / a tutorial - how to track where your users came from |
Wouldn't it be easier to add into the I assume that you opted out of that... and opted to add them as properties only when creating the person to save on DB traffic? Yet wouldn't we be able to handle that one extra Postgres query? ... and optimise in the future if necessary? If the initial props don't change, we won't add a new row into ClickHouse after all. Adding a filter for just Thinking a step further, the idea was for the maxmind plugin to also set Also, I tried re-implementing this as a plugin and ended up with this: // UTM and Initial Properties Plugin
const campaignParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_content', 'utm_term']
const initialParams = ['$browser', '$browser_version', '$current_url', '$os', '$referring_domain', '$referrer']
const allParams = [...initialParams, ...campaignParams]
function processEvent(event, { config }) {
if (event.event !== '$pageview') {
return event
}
let setProperties = false
const $set = {}
const $setOnce = {}
for (param of allParams) {
if (typeof event.properties?.[param] !== 'undefined') {
$setOnce[`\$initial_${param.replace('$', '')}`] = event.properties[param]
setProperties = true
}
}
for (param of campaignParams) {
if (typeof event.properties?.[param] !== 'undefined') {
$set[param] = event.properties[param]
setProperties = true
}
}
if (setProperties) {
event.properties['$set'] = { ...event.properties['$set'], ...$set }
event.properties['$set_once'] = { ...event.properties['$set_once'], ...$setOnce }
}
return event
} Maybe it could even be just a plugin? Enabled by default for everyone? That would test the cloud system to its limits and require lazy VMs, but... maybe? :) |
The problem isn't postgres queries, it's clickhouse queries. We'd insert a new person for each event, and merging people is super expensive on clickhouse. |
I must be missing something then. We bail from creating anything in clickhouse if nothing changes in the props. If initial props have already been set, nothing will have changed. |
Oh didn't realise this! In that case I think this makes sense. In terms of plugins, I think this should be enabled for everyone and not sure why anyone would want to disable it, so it's a core bit of functionality. I don't want to clog the plugins list with something like this. Can we hide plugins and enable as default? |
For sure, but then let's simplify this to just use $set_once? |
Yep will do. |
…Hog/plugin-server into set-utm-and-other-initial-on-person
Done! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
I changed the type of properties
and there's a flaky test that failed earlier. Once they're green I'll merge and release a new version.
…ver#214) * Set UTM etc when creating persons * fix test * Use slightly faster COUNT(*) * Rename userShouldSetUTM to ensurePersonUpdateOnUtm for clarity * Refactor ensurePersonUpdateOnUtm a bit * Prettier * only use $set and $set_once * fix * Add new prop * remove new team prop * use sets * fix type Co-authored-by: Michael Matloka <[email protected]> Co-authored-by: Marius Andra <[email protected]>
Changes
This will automatically set UTM tags on the person initially and every time there's a new utm tag (ie first-touch and last-touch). It'll also set some other properties as initial (current url, browser etc).
Still need to test this but does this approach make sense?
The only downside of this is that if your first event for a user is a server event the initial properties won't get set. This might happen if your sign up form is on a marketing website that doesn't have posthog, which sends an event to a server that does. I think this is an unlikely enough scenario that this is worth it.
Checklist