diff --git a/src/query-jobs/boostr_impressions.js b/src/query-jobs/boostr_impressions.js index b6df271..9b924ba 100644 --- a/src/query-jobs/boostr_impressions.js +++ b/src/query-jobs/boostr_impressions.js @@ -4,18 +4,20 @@ * @param {ExportConfig} config */ export default async function job(config) { + const ds = process.env.BIGQUERY_DATASET; const query = ` SELECT - FORMAT_TIMESTAMP("%m/%d/%Y", timestamp) AS Date, - external_id AS \`Ad Server Line\`, - count(*) AS Impressions - FROM ${process.env.BIGQUERY_DATASET}.dt_impressions - INNER JOIN ${process.env.BIGQUERY_DATASET}.flights ON (flight_id = id) - WHERE timestamp >= ? - AND timestamp < ? - AND is_duplicate = false - AND creative_id IS NOT NULL - AND integration_id IN (${config.integrationIds.join(", ")}) + FORMAT_TIMESTAMP("%m/%d/%Y", i.timestamp) AS Date, + COALESCE(x.external_id, f.external_id) AS \`Ad Server Line\`, + COUNT(*) AS Impressions + FROM ${ds}.dt_impressions i + INNER JOIN ${ds}.flights f ON (i.flight_id = f.id) + LEFT JOIN ${ds}.flight_collection_external_ids x ON (f.id = x.flight_id AND i.feeder_podcast = x.podcast_id) + WHERE i.timestamp >= ? + AND i.timestamp < ? + AND i.is_duplicate = FALSE + AND i.creative_id IS NOT NULL + AND f.integration_id IN (${config.integrationIds.join(", ")}) GROUP BY Date, \`Ad Server Line\` ORDER BY Date ASC, \`Ad Server Line\` ASC `;