Obtenir toutes les campagnes
function getAllCampaigns() {
// AdsApp.campaigns() will return all campaigns that are not removed by
// default.
const campaignIterator = AdsApp.campaigns().get();
console.log(`Total campaigns found : ${campaignIterator.totalNumEntities()}`);
return campaignIterator;
}
Retrouver une campagne à partir de son nom
function getCampaignByName(name) {
const campaignIterator = AdsApp.campaigns()
.withCondition(`campaign.name = "${name}"`)
.get();
if (campaignIterator.hasNext()) {
const campaign = campaignIterator.next();
console.log(`Campaign Name: ${campaign.getName()}`);
console.log(`Enabled: ${campaign.isEnabled()}`);
console.log(`Bidding strategy: ${campaign.getBiddingStrategyType()}`);
console.log(`Ad rotation: ${campaign.getAdRotationType()}`);
console.log(`Start date: ${formatDate(campaign.getStartDate())}`);
console.log(`End date: ${formatDate(campaign.getEndDate())}`);
return campaign;
} else {
throw new Error(`No campaign named "${name}" found`);
}
}
function formatDate(date) {
function zeroPad(number) { return Utilities.formatString('%02d', number); }
return (date == null) ? 'None' : zeroPad(date.year) + zeroPad(date.month) +
zeroPad(date.day);
}
Obtenir les statistiques d'une campagne
function getCampaignStats(name) {
const campaignIterator = AdsApp.campaigns()
.withCondition(`campaign.name = "${name}"`)
.get();
if (campaignIterator.hasNext()) {
const campaign = campaignIterator.next();
// You can also request reports for pre-defined date ranges. See
// https://developers.google.com/google-ads/scripts/docs/reference/adsapp/adsapp_campaign#getStatsFor_1,
// DateRangeLiteral section for possible values.
const stats = campaign.getStatsFor('LAST_MONTH');
console.log(`${campaign.getName()}: ${stats.getClicks()} clicks, ${stats.getImpressions()} impressions`);
return stats;
} else {
throw new Error(`No campaign named "${name}" found`);
}
}
Mettre en veille une campagne
function pauseCampaign(name) {
const campaignIterator = AdsApp.campaigns()
.withCondition(`campaign.name = "${name}"`)
.get();
if (campaignIterator.hasNext()) {
const campaign = campaignIterator.next();
campaign.pause();
} else {
throw new Error(`No campaign named "${name}" found`);
}
}
Obtenir les modificateurs d'enchères en fonction des appareils d'une campagne
function getCampaignBidModifiers(name) {
const campaignIterator = AdsApp.campaigns()
.withCondition(`campaign.name = "${name}"`)
.get();
if (campaignIterator.hasNext()) {
const campaign = campaignIterator.next();
console.log('Campaign name: ' + campaign.getName());
const platforms = {};
for (const platform of campaign.targeting().platforms()) {
console.log(`${platform.getName()} bid modifier: ${platform.getBidModifier()}`);
platforms[platform.getName()] = platform.getBidModifier();
}
return platforms;
} else {
throw new Error(`No campaign named "${name}" found`);
}
}