2424
2525In order to run this test, make sure you followed steps:
26261. Login to https://analytics.google.com
27- 2. In the settings section create an account and save its ID in the variable GA_ACCOUNT_ID.
27+ 2. In the settings section create an account and save its ID in the Google Cloud Secret Manager with id
28+ saved in the constant GOOGLE_ANALYTICS_ACCOUNT_SECRET_ID.
28293. In the settings section go to the Property access management page and add your service account email with
2930Editor permissions. This service account should be created on behalf of the account from the step 1.
30314. Make sure Google Analytics Admin API is enabled in your GCP project.
31325. Create Google Ads account and link it to your Google Analytics account in the GA admin panel.
32- 6. Associate the Google Ads account with a property, and save this property's id in the variable
33- GA_GOOGLE_ADS_PROPERTY_ID .
33+ 6. Associate the Google Ads account with a property, and save this property's id in the Google Cloud Secret
34+ Manager with id saved in the constant GOOGLE_ADS_PROPERTY_ID .
3435"""
3536
3637from __future__ import annotations
4142from datetime import datetime
4243
4344from google .analytics import admin_v1beta as google_analytics
45+ from google .cloud .exceptions import NotFound
4446
4547from airflow .decorators import task
4648from airflow .models import Connection
4749from airflow .models .dag import DAG
4850from airflow .operators .bash import BashOperator
51+ from airflow .providers .google .cloud .hooks .secret_manager import GoogleCloudSecretManagerHook
4952from airflow .providers .google .marketing_platform .operators .analytics_admin import (
5053 GoogleAnalyticsAdminCreateDataStreamOperator ,
5154 GoogleAnalyticsAdminCreatePropertyOperator ,
5962from airflow .utils .trigger_rule import TriggerRule
6063
6164ENV_ID = os .environ .get ("SYSTEM_TESTS_ENV_ID" )
62- DAG_ID = "example_google_analytics_admin "
65+ DAG_ID = "google_analytics_admin "
6366
6467CONNECTION_ID = f"connection_{ DAG_ID } _{ ENV_ID } "
65- ACCOUNT_ID = os .environ .get ("GA_ACCOUNT_ID" , "123456789" )
68+ GOOGLE_ANALYTICS_ACCOUNT_SECRET_ID = "google_analytics_account_id"
69+ GOOGLE_ADS_PROPERTY_SECRET_ID = "google_ads_property_id"
6670PROPERTY_ID = "{{ task_instance.xcom_pull('create_property')['name'].split('/')[-1] }}"
6771DATA_STREAM_ID = "{{ task_instance.xcom_pull('create_data_stream')['name'].split('/')[-1] }}"
68- GA_GOOGLE_ADS_PROPERTY_ID = os .environ .get ("GA_GOOGLE_ADS_PROPERTY_ID" , "123456789" )
6972GA_ADS_LINK_ID = "{{ task_instance.xcom_pull('list_google_ads_links')[0]['name'].split('/')[-1] }}"
7073
7174log = logging .getLogger (__name__ )
7275
76+
77+ def get_secret (secret_id : str ) -> str :
78+ hook = GoogleCloudSecretManagerHook ()
79+ if hook .secret_exists (secret_id = secret_id ):
80+ return hook .access_secret (secret_id = secret_id ).payload .data .decode ()
81+ raise NotFound ("The secret '%s' not found" , secret_id )
82+
83+
7384with DAG (
7485 DAG_ID ,
7586 schedule = "@once" , # Override to match your needs,
@@ -102,6 +113,18 @@ def setup_connection(**kwargs) -> None:
102113
103114 setup_connection_task = setup_connection ()
104115
116+ @task
117+ def get_google_analytics_account_id ():
118+ return get_secret (secret_id = GOOGLE_ANALYTICS_ACCOUNT_SECRET_ID )
119+
120+ get_google_analytics_account_id_task = get_google_analytics_account_id ()
121+
122+ @task
123+ def get_google_ads_property_id ():
124+ return get_secret (secret_id = GOOGLE_ADS_PROPERTY_SECRET_ID )
125+
126+ get_google_ads_property_id_task = get_google_ads_property_id ()
127+
105128 # [START howto_marketing_platform_list_accounts_operator]
106129 list_accounts = GoogleAnalyticsAdminListAccountsOperator (
107130 task_id = "list_account" ,
@@ -114,7 +137,7 @@ def setup_connection(**kwargs) -> None:
114137 create_property = GoogleAnalyticsAdminCreatePropertyOperator (
115138 task_id = "create_property" ,
116139 analytics_property = {
117- "parent" : f"accounts/{ ACCOUNT_ID } " ,
140+ "parent" : f"accounts/{ get_google_analytics_account_id_task } " ,
118141 "display_name" : "Test display name" ,
119142 "time_zone" : "America/Los_Angeles" ,
120143 },
@@ -158,15 +181,15 @@ def setup_connection(**kwargs) -> None:
158181 # [START howto_marketing_platform_list_google_ads_links]
159182 list_google_ads_links = GoogleAnalyticsAdminListGoogleAdsLinksOperator (
160183 task_id = "list_google_ads_links" ,
161- property_id = GA_GOOGLE_ADS_PROPERTY_ID ,
184+ property_id = get_google_ads_property_id_task ,
162185 gcp_conn_id = CONNECTION_ID ,
163186 )
164187 # [END howto_marketing_platform_list_google_ads_links]
165188
166189 # [START howto_marketing_platform_get_google_ad_link]
167190 get_ad_link = GoogleAnalyticsAdminGetGoogleAdsLinkOperator (
168191 task_id = "get_ad_link" ,
169- property_id = GA_GOOGLE_ADS_PROPERTY_ID ,
192+ property_id = get_google_ads_property_id_task ,
170193 google_ads_link_id = GA_ADS_LINK_ID ,
171194 gcp_conn_id = CONNECTION_ID ,
172195 )
@@ -180,7 +203,7 @@ def setup_connection(**kwargs) -> None:
180203
181204 (
182205 # TEST SETUP
183- setup_connection_task
206+ [ setup_connection_task , get_google_analytics_account_id_task , get_google_ads_property_id_task ]
184207 # TEST BODY
185208 >> list_accounts
186209 >> create_property
0 commit comments