Deng-3472 sync suppression list to mofo (#5503)

* status quo

* DENG-3472 sync suppression list back to campaign monitor

* remove hard coded date
This commit is contained in:
Leli 2024-05-10 21:30:43 +02:00 коммит произвёл GitHub
Родитель 07798d7db3
Коммит 982698b991
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
8 изменённых файлов: 127 добавлений и 0 удалений

Просмотреть файл

@ -0,0 +1,18 @@
friendly_name: New Suppression List Entries For MoFO
description: |-
This table contains all the entries that need to be uploaded to the suppression list in Campaign Monitor.
The main suppression list is build from suppressions from different sources like Campaign Monitor, Acoustic and Braze.
The query compares the entries on the main suppression list
(after March 31 2024 which is the current cutoff date for the suppression list)
with entries in the suppression list already in Campaign Monitor.
Only those missing entries need to be added to the suppression list in Campaign Monitor.
owners:
- leli@mozilla.com
labels:
incremental: false
owner1: leli
scheduling:
dag_name: bqetl_marketing_suppression_list
date_partition_parameter: null
bigquery: null
references: {}

Просмотреть файл

@ -0,0 +1,9 @@
SELECT
main.email
FROM
`moz-fx-data-shared-prod.marketing_suppression_list_derived.main_suppression_list_v1` AS main
LEFT JOIN
`moz-fx-data-shared-prod.marketing_suppression_list_external.campaign_monitor_suppression_list_v1` AS current_mofo
ON main.email = current_mofo.email
WHERE
current_mofo.email IS NULL

Просмотреть файл

@ -0,0 +1,4 @@
fields:
- name: email
type: STRING
mode: NULLABLE

Просмотреть файл

@ -0,0 +1,15 @@
friendly_name: Send Suppression List Update To CampaignMonitor
description: |-
This python script gets the table new_suppression_list_entries_for_mofo_v1 and uploads the result to the Campaign Monitor API.
owners:
- leli@mozilla.com
labels:
incremental: false
owner1: leli
scheduling:
dag_name: bqetl_marketing_suppression_list
arguments:
- --api_key={{ var.value.campaign_monitor_api_key }}
- --client_id={{ var.value.campaign_monitor_client_id }}
bigquery: null
references: {}

Просмотреть файл

@ -0,0 +1,57 @@
"""Upload changes in Suppression list to Campaign Monitor API."""
from typing import List
import requests
import rich_click as click
from google.cloud import bigquery
from requests.auth import HTTPBasicAuth
BASE_URL = "https://api.createsend.com/api/v3.3/clients"
def list_new_suppressions() -> List[str]:
"""Download the new_suppression_list_entries_for_mofo_v1 table into an array."""
client = bigquery.Client()
query = """
SELECT
email
FROM
`moz-fx-data-shared-prod.marketing_suppression_list_external.new_suppression_list_entries_for_mofo_v1`
"""
data = client.query(query).result()
return [row["email"] for row in list(data)]
def upload_new_suppressions_to_campaign_monitor(
base_url: str, client_id: str, api_key: str, emails: List[str]
) -> None:
"""Add emails to suppression list in Campaign Monitor."""
auth = HTTPBasicAuth(api_key, "")
add_suppression_list_url = f"{base_url}/{client_id}/suppress.json"
payload = {"EmailAddresses": emails}
response = requests.post(url=add_suppression_list_url, auth=auth, json=payload)
response.raise_for_status()
@click.command
@click.option(
"--api_key",
required=True,
help="Campaign Monitor API key to use for authentication.",
)
@click.option(
"--client_id",
required=True,
help="Client Id for Campaign Monitor.",
)
def main(api_key: str, client_id: str, base_url: str) -> None:
"""Download list of new suppression from BigQuery and upload it to the Campaign Monitor API."""
emails = list_new_suppressions()
upload_new_suppressions_to_campaign_monitor(
base_url=base_url, api_key=api_key, client_id=client_id, emails=emails
)
if __name__ == "__main__":
main(base_url=BASE_URL)

Просмотреть файл

@ -0,0 +1,6 @@
# expect
---
# user 1 before cutoff date
# user 2 after cutoff date already in campaign monitor
# user 3 after cutoff date, not in campaign monitor
- email: user_3@example.mail

Просмотреть файл

@ -0,0 +1,11 @@
# main suppression list
---
# user 1 before cutoff date
- email: user_1@example.mail
suppressed_timestamp: 2020-01-01
# user 2 after cutoff date already in campaign monitor
- email: user_2@example.mail
suppressed_timestamp: 2024-04-01
# user 3 after cutoff date, not in campaign monitor
- email: user_3@example.mail
suppressed_timestamp: 2024-04-02

Просмотреть файл

@ -0,0 +1,7 @@
# campaign monitor suppression list
---
# user 1 before cutoff date
- email: user_1@example.mail
# user 2 after cutoff date already in campaign monitor
- email: user_2@example.mail
# user 3 after cutoff date, not in campaign monitor