import { omitUndefinedKeys } from "./omitUndefinedKeys.ts"; /** * Full UTM configuration object. */ export type UtmParams = { /** * The website/platform the visitor is coming from. */ source: string; /** * The type of marketing channel (e.g. cpc, email, affiliate, social, * or similar). */ medium: string; /** * The name of the campaign or product description. In our case, this is * usually the name of the app. */ campaign: string; /** * Optionally, provide an identifier for the place from which the link was * clicked. E.g. footer, join, etc. */ content?: string; /** * Optionally, identify paid keywords. We usually do not use this. */ term?: string; }; /** * UTM Params configuration that is appropriate to set at the link level, * if app-level defaults have been set. */ export type LinkUtmParams = Pick; /** * Returns URL Search params with provided UTM configuration. * * Most of the time, you probably want to set up some defaults using * {@link createUtm}. This function is intended for special cases. */ export function utm(params: UtmParams) { return new URLSearchParams( omitUndefinedKeys({ utm_source: params.source, utm_medium: params.medium, utm_campaign: params.campaign, utm_content: params.content, utm_term: params.term, }), ); } /** * A factory for the {@link utm} function. Use it to set sensible defaults for * further use of the returned function. * * @example * ```ts * const utm = createUtm({ * source: "indietabletopclub", * medium: "referral", * campaign: "spacegitsapp", * }); * * utm() // => URLSearchParams that inlude the above config * * utm({ campaign: "foo", content: "bar" }) // Sets optional fiels and overrides * ``` */ export function createUtm(defaults: UtmParams) { return function (params?: Partial) { return utm({ ...defaults, ...(params && omitUndefinedKeys(params)), }); }; }