Snowflake (version 2)

Create Scope3 API Integration

create or replace api integration scope3_api
    api_provider = google_api_gateway
    google_audience = ''
    api_allowed_prefixes = ('https://scope3-snowflake-api-gateway-5qbjzj9o.uc.gateway.dev')
    enabled = true;

Create Scope3 External Function

Function Call

create or replace external function scope3_v2(
  impressions int,
  date date,
  inventory_id string,
  device_type string,
  channel string,
  country string,
  region string,
  creative_format string,
  payload_size int,
  duration_seconds int,
  network string,
  seller string,
  buying_method string,
  deal_id string)
    returns OBJECT
    api_integration = scope3_api
    headers = (
        'AccessClientId'='****',
        'AccessClientSecret'='****'
    )
    as 'https://scope3-snowflake-api-gateway-5qbjzj9o.uc.gateway.dev/api/v1/snowflake';
FieldrequiredDescriptionExample
impressionsSum of impressions for these dimensions10000
dateDates must be in the past (i.e. prior to today). Dates should be in the UTC timezone in one of the following formats: YYYY-MM-DD, YYYYMMDD, or YYYY/MM/DD2023-04-01
inventory_idThe inventory ID field is an alternative to site.domain and app.storeId. This field can accept either a domain or app ID. When channel is not specified, we will use inventoryId to make an informed guess.nytimes.com
device_typeThe consumer’s device: phone, tablet, pc, or tv. If not provided, we’ll infer the device type from the the channel or the device field.pc
channelOptions: display-app, display-web, streaming-video, social

display-web: Display ads, including outstream video, on a website

display-app: Display ads, including outstream video, on a mobile app. Excludes streaming and social apps

streaming-video: Streaming video including CTV and YouTube. Includes in app or browser based streaming. Currently in a closed beta.

social: Includes in app or browser based social properties. Currently in closed beta
display-web
countryThis is the country ISO 3166-2 alpha-2 code. e.g. USUS
regionThe 2 or 3 letter region code - only supported for Canada, US, and AustraliaNY
creative_formatOptions: banner, video, text

Banner uses payload size and conventional model.

Video uses duration and power model.
Text returns 0
video
payload_sizeThe number of bytes transferred to fully display the creative. Only used for banner and video format.3024
duration_secondsTime the creative plays. Only used for video format.15
networkoptions: fixed, mobile
The networking connection used by the end user's device.
fixed
sellerName of the seller entity that sold the mediaxandr
buying_methodoptions: programmatic-open, programmatic-pmp, programmatic-guaranteed, direct, direct-takeover

The method used to buy the impressions
programmatic-open
deal_idSeller deal identifierdeal-1234

Response

The function returns the following object. Each field may be missing/null depending if the data is available.

If total_emissions and error is null then scope3 does noes not have information for this impression.

{
  "ad_selection_emissions": "<float|null>",
  "domain_coverage": "<modeled|missing|unknown>",
  "gmp_eligible": "<true|false>",
  "matched_channel": "<display-web|display-app|streaming-video|social>",
  "matched_property": "<string>",
  "media_distribution_emissions": "<float>",
  "supply_directness": "<int>",
  "total_emissions": "<float>",
  "error": "<string|null>"
}
FieldDescriptionExample
ad_selection_emissionsAd tech including servers and cloud computing; analytics; network traffic; storage; data providers; and vendor overhead. In grams of CO₂e.300.99
domain_coveragemodeled - domains/apps are in the dataset with reasonable confidence of accuracy.
missing - domains/apps have not been modeled yet and will automatically be queued for analysis.
unknown - domains/apps appear to have errors. e.g. A site that has an invalid domain name.
modeled
gmp_eligibleImpressions are GMP eligible if the gCO₂e ad selection emissions per impression is lower than benchmark emissions data for the associated country and channel. If benchmarks are unavailable for a specific country then worldwide benchmarks will be used. [GMP sellers only]true
matched_channelCreative Emissions in gCO2e100.33
matched_propertyModeled domains are in the dataset with reasonable confidence of accuracy. Missing domains have not been modeled yet and will automatically be queued for analysis.modeled
media_distribution_emissionsMedia delivery including CMS; CDNs and hosting services; publishing overhead including employee and office expenses; data transfer. In grams of CO₂e.50.2
supply_directnessMaximum depth of ad selection path to the seller based on buying method [Programmatic Insights customers only]3
total_emissionsTotal footprint of the campaign (sum of above) in grams of CO₂e.400.2
errornull / missing if no error. If error this will contain a string error messagenull

Example Usage

Select a single Example Row

SELECT scope3_v2(
  1000, -- impressions
  DATE('2023-04-20'), -- date
  'nytimes.com', -- inventory_id
  'pc', -- device_type
  null, -- channel
  'US', -- country
  'NY', -- region
  'banner', -- creative_format
  null, -- payload_size
  null, --duration_seconds
  'fixed', -- network
  null, -- seller
  null, -- buying_method
  'deal-1234', -- deal_id
);

Real life usage

SELECT
	d.app_url,
	d.country,
	d.impressions,
  SELECT scope3_v2(
  	d.impressions, -- impressions
  	d.date, -- date
  	d.app_url, -- inventory_id
  	d.device_type, -- device_type
  	null, -- channel
  	d.country, -- country
  	null, -- region
  	null, -- creative_format
  	null, -- payload_size
  	null, --duration_seconds
  	null, -- network
    null, -- seller
  	null, -- buying_method
  	null, -- deal_id
	):total_emissions
FROM
	delivery_data d;

How to extract single fields

SELECT
	res:total_emissions,
	res:base_emissions,
	res:supply_path_emissions,
	res:creative_emissions
FROM
	(
    SELECT scope3_v2(
      1000, -- impressions
      DATE('2023-04-20'), -- date
      'nytimes.com', -- inventory_id
      'pc', -- device_type
      null, -- channel
      'US', -- country
      'NY', -- region
      'banner', -- creative_format
      null, -- payload_size
      null, --duration_seconds
      'fixed', -- network
      null, -- seller
      null, -- buying_method
      'deal-1234', -- deal_id
		) res
  )
;

Dealing with errors

If your use case requires special handling for errors, you can use IFF and IS_NULL_VALUE.

SELECT
	parse_json('{error: NULL, total_emissions: 123}') as V,
  V:error,
	V:total_emissions,
	IS_NULL_VALUE(v:error),
	IS_NULL_VALUE(v:total_emissions),
	IFF(IS_NULL_VALUE(v:non_existing_key) is null, 'Key does not exist', 'Key exists'),
	IFF(IS_NULL_VALUE(v:error) is null, 'Key does not exist', 'Key exists'),
	IFF(IS_NULL_VALUE(v:total_emissions) is null, 'Key does not exist', 'Key exists')
;