diff --git a/integration_tests/.scripts/integration_tests.sh b/integration_tests/.scripts/integration_tests.sh index 1add026..5949b3e 100755 --- a/integration_tests/.scripts/integration_tests.sh +++ b/integration_tests/.scripts/integration_tests.sh @@ -45,8 +45,6 @@ for db in ${DATABASES[@]}; do eval "dbt run --target $db --vars '{snowplow__enable_youtube: false, snowplow__enable_whatwg_media: false, snowplow__enable_whatwg_video: false, snowplow__enable_media_player_v1: false}'" || exit 1; - - echo "Snowplow media player integration tests: Execute models - run 1/6" eval "dbt run --target $db --full-refresh --vars '{snowplow__allow_refresh: true}'" || exit 1; @@ -69,6 +67,21 @@ for db in ${DATABASES[@]}; do eval "dbt test --target $db" || exit 1; + echo "Snowplow media player integration tests: Testing ad views passthrough - mixed configuration" + eval "dbt run --select +snowplow_media_player_media_ad_views_this_run --target $db --full-refresh --vars '{ + snowplow__allow_refresh: true, + snowplow__enable_media_ad: true, + snowplow__backfill_limit_days: 999, + snowplow__ad_views_passthroughs: [ + \"v_collector\", + {\"sql\": \"v_tracker || app_id\", \"alias\": \"tracker_app_id\", \"agg\": \"max\"}, + {\"sql\": \"v_tracker || app_id\", \"alias\": \"tracker_app_id_1\", \"agg\": \"min\"}, + {\"sql\": \"v_collector\", \"alias\": \"tracker_app_id_2\", \"agg\": \"min\"} + ] + }'" || exit 1; + + eval "dbt test --target $db --select snowplow_media_player_media_ad_views_this_run" || exit 1; + echo "Snowplow media player integration tests: All tests passed" -done +done \ No newline at end of file diff --git a/models/media_ad_views/scratch/snowplow_media_player_media_ad_views_this_run.sql b/models/media_ad_views/scratch/snowplow_media_player_media_ad_views_this_run.sql index a31b247..c35849f 100644 --- a/models/media_ad_views/scratch/snowplow_media_player_media_ad_views_this_run.sql +++ b/models/media_ad_views/scratch/snowplow_media_player_media_ad_views_this_run.sql @@ -44,17 +44,39 @@ events_this_run as ( ,ev.media_ad_break__break_id as ad_break_id ,ev.media_ad__ad_id as ad_id + + {%- set non_agg_identifiers = [] -%} + {%- for identifier in var('snowplow__ad_views_passthroughs', []) -%} + {%- if identifier is not mapping or 'agg' not in identifier -%} + {%- do non_agg_identifiers.append(identifier) -%} + {%- endif -%} + {%- endfor -%} + + {%- if var('snowplow__ad_views_passthroughs', []) -%} {%- set passthrough_names = [] -%} + {%- set agg_identifiers = [] -%} + {%- for identifier in var('snowplow__ad_views_passthroughs', []) %} - {# Check if it is a simple column or a sql+alias #} - {%- if identifier is mapping -%} - ,{{identifier['sql']}} as {{identifier['alias']}} - {%- do passthrough_names.append(identifier['alias']) -%} - {%- else -%} - ,ev.{{identifier}} - {%- do passthrough_names.append(identifier) -%} - {%- endif -%} + {# Check if it is a mapping with no agg attribute #} + {%- if identifier is mapping and 'agg' not in identifier -%} + ,{{identifier['sql']}} as {{identifier['alias']}} + {%- do passthrough_names.append(identifier['alias']) -%} + + {# Check if it is a mapping with agg attribute #} + {%- elif identifier is mapping and 'agg' in identifier -%} + {%- do agg_identifiers.append(identifier) -%} + + {# Handle simple column names #} + {%- else -%} + ,ev.{{identifier}} + {%- do passthrough_names.append(identifier) -%} + {%- endif -%} + {% endfor -%} + + {# Add aggregated columns after the main selection #} + {%- for agg_col in agg_identifiers %} + ,{{agg_col['agg']}}({{agg_col['sql']}}) as {{agg_col['alias']}} {% endfor -%} {%- endif %} @@ -81,7 +103,7 @@ events_this_run as ( ,{{ snowplow_utils.get_string_agg('original_session_identifier', 'ev', is_distinct=True) }} as domain_sessionid_array from events_this_run as ev - {{ dbt_utils.group_by(n=9+(var('snowplow__ad_views_passthroughs', [])|length)) }} + {{ dbt_utils.group_by(n=9+non_agg_identifiers|length) }} ) @@ -123,6 +145,10 @@ select {%- for col in passthrough_names %} , p.{{col}} {%- endfor -%} + {%- for agg_col in agg_identifiers %} + , p.{{agg_col['alias']}} + {%- endfor -%} + {%- endif %} from prep as p