private static final String ADVERTISER_ID = "INSERT_ADVERTISER_COMPANY_ID_HERE";
public static void runExample(DfpServices dfpServices, DfpSession session, long advertiserId)
throws Exception {
// Get the CreativeService.
CreativeServiceInterface creativeService =
dfpServices.get(session, CreativeServiceInterface.class);
// Create creative size.
Size size = new Size();
size.setWidth(300);
size.setHeight(250);
size.setIsAspectRatio(false);
// Use the image banner with optional third party tracking template.
// To determine what other creative templates exist,
// run GetAllCreativeTemplates.java.
long creativeTemplateId = 10000680L;
// Create a template creative.
TemplateCreative templateCreative = new TemplateCreative();
templateCreative.setName("Template creative #" + new Random().nextInt(Integer.MAX_VALUE));
templateCreative.setAdvertiserId(advertiserId);
templateCreative.setCreativeTemplateId(creativeTemplateId);
templateCreative.setSize(size);
// Create the asset variable value.
AssetCreativeTemplateVariableValue assetVariableValue =
new AssetCreativeTemplateVariableValue();
assetVariableValue.setUniqueName("Imagefile");
assetVariableValue.setAssetByteArray(Media.getMediaDataFromUrl(
"http://www.google.com/intl/en/adwords/select/images/samples/inline.jpg"));
// Filenames must be unique.
assetVariableValue.setFileName(
String.format("image%s.jpg", new Random().nextInt(Integer.MAX_VALUE)));
// Create the image width variable value.
LongCreativeTemplateVariableValue imageWidthVariableValue =
new LongCreativeTemplateVariableValue();
imageWidthVariableValue.setUniqueName("Imagewidth");
imageWidthVariableValue.setValue(300L);
// Create the image height variable value.
LongCreativeTemplateVariableValue imageHeightVariableValue =
new LongCreativeTemplateVariableValue();
imageHeightVariableValue.setUniqueName("Imageheight");
imageHeightVariableValue.setValue(250L);
// Create the URL variable value.
UrlCreativeTemplateVariableValue urlVariableValue =
new UrlCreativeTemplateVariableValue();
urlVariableValue.setUniqueName("ClickthroughURL");
urlVariableValue.setValue("www.google.com");
// Create the target window variable value.
StringCreativeTemplateVariableValue targetWindowVariableValue =
new StringCreativeTemplateVariableValue();
targetWindowVariableValue.setUniqueName("Targetwindow");
targetWindowVariableValue.setValue("__blank");
// Set the creative template variables.
templateCreative.setCreativeTemplateVariableValues(new BaseCreativeTemplateVariableValue[] {
assetVariableValue, imageWidthVariableValue, imageHeightVariableValue, urlVariableValue,
targetWindowVariableValue});
// Create the creative on the server.
Creative[] creatives =
creativeService.createCreatives(new Creative[] {templateCreative});
for (Creative createdCreative : creatives) {
System.out.printf("A creative with ID \"%d\", name \"%s\", and type \"%s\""
+ " was created and can be previewed at: %s\n", createdCreative.getId(),
createdCreative.getName(), createdCreative.getCreativeType(),