keyword_

20 views
Skip to first unread message

elite growth

unread,
Jun 27, 2024, 8:06:04 PM (5 days ago) Jun 27
to Google Ads Scripts Forum
Hi,

I'm using the script below to pull campaign data with the aim of including; campaign name, adset, ad, keyword and match type but im getting an error anytime I preview the script. I'm getting the keyword and the keyword type, but campaign name, adset name, and ad name is missing. It's weird because this same script is working across other accounts and the bug seems to be specific with one account. Thanks!

script:
function main() {
    var TrackingTemplate = "{lpurl}?utm_source=adwords&utm_medium={AdName}&utm_campaign={CampaignName}&utm_content={AdGroupName}&utm_keyword={keyword}&utm_matchtype={matchtype}&campaign_id={campaignid}&ad_group_id={adgroupid}&ad_id={creative}";

    var _CAMPAIGN_CONTAINS = "";
    var _ADGROUP_CONTAINS = "";
    var STATUS = "ENABLED";

    if (TrackingTemplate.search(/{AdGroupName}|{CampaignName}|{AdName}/g) == -1) {
        Logger.log("Enter at least one of the {CampaignName} or {AdGroupName} or {AdName} parameter in the tracking template");
        return
    }
   
    if (TrackingTemplate.search("{AdGroupName}") > 0) {
        var adgroupIterator = {
            hasNext: function() {
                return false
            }
        }
        if (_ADGROUP_CONTAINS == "" && _CAMPAIGN_CONTAINS == "") {
            adgroupIterator = AdsApp.adGroups().withCondition("Status = " + STATUS).get();
        } else if (_ADGROUP_CONTAINS == "" && _CAMPAIGN_CONTAINS !== "") {
            adgroupIterator = AdsApp.adGroups().withCondition("CampaignName contains '" + _CAMPAIGN_CONTAINS + "'").withCondition("Status = " + STATUS).get();
        } else if (_ADGROUP_CONTAINS !== "" && _CAMPAIGN_CONTAINS !== "") {
            adgroupIterator = AdsApp.adGroups().withCondition("CampaignName contains '" + _CAMPAIGN_CONTAINS + "'").withCondition("Name contains '" + _ADGROUP_CONTAINS + "'").withCondition("Status = " + STATUS).get();
        } else if (_ADGROUP_CONTAINS !== "" && _CAMPAIGN_CONTAINS == "") {
            adgroupIterator = AdsApp.adGroups().withCondition("Name contains '" + _ADGROUP_CONTAINS + "'").withCondition("Status = " + STATUS).get();
        }

        if (!adgroupIterator.hasNext()) {
            Logger.log("No Campaigns/Adgroups matched with this condition");
            return
        }
        while (adgroupIterator.hasNext()) {
            var adgroup = adgroupIterator.next();
            var adgrouptemplate = TrackingTemplate.replace(/{AdGroupName}/g, adgroup.getName().replace(/\s/g, '%20'))
            if (TrackingTemplate.search("{CampaignName}") > 0) {
                adgrouptemplate = adgrouptemplate.replace(/{CampaignName}/g, adgroup.getCampaign().getName().replace(/\s/g, '%20'))
            }

            if (TrackingTemplate.search("{AdName}") > 0) {
                var adsIterator = adgroup.ads().get();
                while (adsIterator.hasNext()) {
                  var ad = adsIterator.next();
                  var adType = ad.getType();
                  var headline = "";

                  if (ad.getHeadline()) {
                    headline = ad.getHeadline();
                  } else if(ad.isType().expandedTextAd()) {
                    headline = ad.asType().expandedTextAd().getHeadlinePart1();
                  } else if(ad.isType().gmailImageAd()) {
                    headline = ad.asType().gmailImageAd().getName();
                  } else if(ad.isType().gmailMultiProductAd()) {
                    headline = ad.asType().gmailMultiProductAd().getHeadline();
                  } else if(ad.isType().gmailSinglePromotionAd()) {
                    headline = ad.asType().gmailSinglePromotionAd().getHeadline();
                  } else if(ad.isType().html5Ad()) {
                    headline = ad.asType().html5Ad().getName();
                  } else if(ad.isType().imageAd()) {
                    headline = ad.asType().imageAd().getName();
                  } else if(ad.isType().responsiveDisplayAd()) {
                    headline = ad.asType().responsiveDisplayAd().getShortHeadline();
                  } else if(ad.isType().responsiveSearchAd()) {
                    var headlines = ad.asType().responsiveSearchAd().getHeadlines();
                    if (headlines && headlines[0].text) {
                      headline = headlines[0].text;
                    }
                  }

                  Logger.log("Headline text : " + headline);

                  if (headline) {
                    adgrouptemplate = adgrouptemplate.replace(/{AdName}/g, headline.replace(/\s/g, '%20'))
                  } else {
                    adgrouptemplate = adgrouptemplate.replace(/{AdName}/g, ad.getId())
                  }
                }
            }
            adgroup.urls().setTrackingTemplate(adgrouptemplate);
            Logger.log(adgroup.getCampaign().getName() + " => " + adgroup.getName() + " => " + adgrouptemplate)
        }
    }
}

Google Ads Scripts Forum

unread,
Jul 1, 2024, 7:45:43 PM (2 days ago) Jul 1
to Google Ads Scripts Forum

Hi,

Thank you for reaching out to the Google Ads Scripts support team.

In order to investigate further on your issue, kindly provide us with the below details.

  • Google Ads account ID/CID
  • Name of the affected script
  • Shareable spreadsheet link if you are using in your script

You can share the requested details via Reply privately to the author option or a direct private reply to this email.

Thanks,
Google Ads scripts team
Reply all
Reply to author
Forward
0 new messages