Created
January 11, 2022 14:16
-
-
Save Zikoel/fd07b8578f4cedb99943281474320a58 to your computer and use it in GitHub Desktop.
Creation of a Budget, Campaign, GroupAd and AdGroupAd resources atomically with opteo library
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { enums, GoogleAdsApi, MutateOperation, toMicros } from 'google-ads-api' | |
import { loadGadsCredentialFromFile } from '../gadsCredentialsValidator' | |
import { inspect } from 'util' | |
const cred = { | |
client_id: '', | |
client_secret: '', | |
developer_token: '', | |
refresh_token: '', | |
login_customer_id: '', | |
customer_id: '' | |
} | |
const accountClient = new GoogleAdsApi({ | |
...cred, | |
}) | |
const customerClient = accountClient.Customer({ | |
...cred, | |
}) | |
// ordered ids | |
const budgetId = '-1' | |
const campaignId = '-2' | |
const groupId = '-3' | |
const adResourceId = '-4' | |
// properties | |
const customerId = cred.customer_id | |
const campaingName = 'test campaing' | |
const budgetEUR = 10 | |
const bidEUR = 0.5 | |
const groupName = 'test group name' | |
const description1 = 'd1' | |
const description2 = 'd2' | |
const headline1 = 'h1' | |
const headline2 = 'h2' | |
const headline3 = 'h3' | |
const path1 = 'p1' | |
const path2 = 'p2' | |
// Yes on here the type inference is not easy but with some work | |
// absolutely possible | |
// poetic license for the any type only for this example | |
const resources: MutateOperation<any>[] = [ | |
{ | |
entity: 'campaign_budget', | |
operation: 'create', | |
resource: { | |
resource_name: `customers/${customerId}/campaignBudgets/${budgetId}`, | |
name: `Budget for ${campaingName}`, | |
amount_micros: toMicros(budgetEUR), | |
explicitly_shared: false, | |
}, | |
}, | |
{ | |
entity: 'campaign', | |
operation: 'create', | |
resource: { | |
campaign_budget: `customers/${customerId}/campaignBudgets/${budgetId}`, | |
resource_name: `customers/${customerId}/campaigns/${campaignId}`, | |
name: campaingName, | |
status: enums.CampaignStatus.ENABLED, | |
manual_cpc: { | |
enhanced_cpc_enabled: false, | |
}, | |
advertising_channel_type: enums.AdvertisingChannelType.SEARCH, | |
network_settings: { | |
target_google_search: true, | |
target_search_network: true, | |
target_content_network: false, | |
target_partner_search_network: false, | |
}, | |
}, | |
}, | |
{ | |
entity: 'ad_group', | |
operation: 'create', | |
resource: { | |
resource_name: `customers/${customerId}/adGroups/${groupId}`, | |
campaign: `customers/${customerId}/campaigns/${campaignId}`, | |
cpc_bid_micros: toMicros(bidEUR), | |
name: groupName, | |
status: enums.AdGroupStatus.ENABLED, | |
}, | |
}, | |
{ | |
entity: 'ad_group_ad', | |
operation: 'create', | |
resource: { | |
ad_group: `customers/${customerId}/adGroups/${groupId}`, | |
resource_name: `customers/${customerId}/adGroupAds/${groupId}~${adResourceId}`, | |
status: enums.AdGroupAdStatus.ENABLED, | |
ad: { | |
final_urls: ['https://www.yoursite.com'], | |
expanded_text_ad: { | |
description: description1, | |
description2: description2, | |
headline_part1: headline1, | |
headline_part2: headline2, | |
headline_part3: headline3, | |
path1: path1, | |
path2: path2, | |
}, | |
}, | |
}, | |
}, | |
] | |
customerClient | |
.mutateResources(resources) | |
.then(response => { | |
console.log(inspect(response, false, 5, true)) | |
}) | |
.catch(err => { | |
console.log(inspect(err, false, 5, true)) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment