From 9af498833e381d48d8c5b371ec6e5659612cde52 Mon Sep 17 00:00:00 2001 From: Karl-Aksel Puulmann Date: Tue, 19 Nov 2024 10:12:39 +0200 Subject: [PATCH] Channels: backfill utm_medium based on click_param_id (#4833) * Backfill utm_medium Follow-up to https://github.com/plausible/analytics/pull/4817 * Update backfill --- CHANGELOG.md | 1 + ...238_backfill_utm_medium_click_id_param.exs | 27 +++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 priv/ingest_repo/migrations/20241118112238_backfill_utm_medium_click_id_param.exs diff --git a/CHANGELOG.md b/CHANGELOG.md index 7090b49ff..dc0f3056f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ All notable changes to this project will be documented in this file. ### Added - Dashboard shows comparisons for all reports +- UTM Medium report and API shows (gclid) and (msclkid) for paid searches when no explicit utm medium present. ### Removed ### Changed diff --git a/priv/ingest_repo/migrations/20241118112238_backfill_utm_medium_click_id_param.exs b/priv/ingest_repo/migrations/20241118112238_backfill_utm_medium_click_id_param.exs new file mode 100644 index 000000000..df0c470a2 --- /dev/null +++ b/priv/ingest_repo/migrations/20241118112238_backfill_utm_medium_click_id_param.exs @@ -0,0 +1,27 @@ +defmodule Plausible.IngestRepo.Migrations.BackfillUtmMediumClickIdParam do + @moduledoc """ + Backfills utm_medium based on referrer_source and click_id_param + """ + use Ecto.Migration + + def up do + execute(fn -> repo().query!(update_query("events_v2")) end) + execute(fn -> repo().query!(update_query("sessions_v2")) end) + end + + def down do + raise "irreversible" + end + + defp update_query(table) do + """ + ALTER TABLE #{table} + UPDATE utm_medium = multiIf( + referrer_source = 'Google' AND click_id_param = 'gclid', '(gclid)', + referrer_source = 'Bing' AND click_id_param = 'msclkid', '(msclkid)', + utm_medium + ) + WHERE empty(utm_medium) AND NOT empty(click_id_param) + """ + end +end