google-apps-scriptdouble-click-advertisingdfareporting

Create Ad and assign creative and placement


I have a campaign in Campaign Manager. Within the campaign is 1 placement and 1 creative (both 160x600 and active)

I want to create an Ad, assign the creative, and then assign the placement using Google Apps Script in Google Sheets.

I've built out the Ad Resource using the API Reference in Google Apps Script. I can get it to create the Ad, if the ad is set to "inactive". However, none of the assignments are done.

When I try to create the Ad with it set to "Active", I get an error:

"API call to dfareporting.ads.insert failed with error: 12032 : An ad cannot be active unless it is assigned to at least one placement."

Randomly I also get this sometimes:

"API call to dfareporting.ads.insert failed with error: 12058 : At least one active creative must be assigned to any active ad. Also, the creative must have "Include in rotation" set to "Yes" in your ad properties."

Code below:

funtion foo() { 
   var startTime = Utilities.formatDate(new Date(), ss.getSpreadsheetTimeZone(), 'yyyy-MM-dd\'T\'HH:mm:ss.SSS\'Z\'');
  var endDate = new Date (new Date(startTime).getYear()+1, new Date(startTime).getMonth(), new Date(startTime).getDate());
  var endTime = Utilities.formatDate(new Date(endDate), ss.getSpreadsheetTimeZone(), 'yyyy-MM-dd\'T\'HH:mm:ss.SSS\'Z\'');

      var adResource = {
        "kind": "dfareporting#ad",
        "active": "true",
        "archived": "false",
        "campaignId": "23237909",
        "name": "testApiAd",
        "creativeRotation": {
          "creativeAssignments": {
            "active": "true",
            "creativeId": "121312158",//.toString(),
            "clickThroughUrl": {
              "defaultLandingPage": "true",
              //"type": "CREATIVE_ROTATION_TYPE_RANDOM",
              //"weightCalculationStrategy": "WEIGHT_STRATEGY_EQUAL"
            }
          }
        },
        "deliverySchedule": {
          "impressionRatio": "1",
          "priority": "AD_PRIORITY_15",
          "hardCutoff": "false"
        },
        "endTime": endTime,
        "startTime": startTime,
        "type": "AD_SERVING_STANDARD_AD",
        "placementAssignments": {
          "active": "true",
          "placementId": "256185010",//.toString(),
          //"sslRequired": "true"
        }
      }

      var newAd = DoubleClickCampaigns.Ads.insert(adResource, profileID);
  return newAd;
}

Is there something I'm not understanding? Or missing completely? According the API Reference, it should be working but I can't figure out why it's not.

Any help would be much appreciated.


Solution

  • Problem

    Ads with active flag set to true should have at least one placementAssignments set.

    Solution

    Take a look at type of the placementAssignments (as well as creativeAssignments) property - it should be a list (meaning that the value should be an instance of Array) while you define it as object. Simply make both placement and creative elements of an Array:

    "placementAssignments": [{
      "active": "true",
      "placementId": "256185010",//.toString(),
      //"sslRequired": "true"
    }]
    
    "creativeRotation": {
      "creativeAssignments": [{
        "active": "true",
        "creativeId": "121312158",//.toString(),
        "clickThroughUrl": {
          "defaultLandingPage": "true",
          //"type": "CREATIVE_ROTATION_TYPE_RANDOM",
          //"weightCalculationStrategy": "WEIGHT_STRATEGY_EQUAL"
        }
      }]
     }
    

    Reference

    1. ad resource overview;