Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add Java example for creating a Demand Gen campaign using API V20#845

Closed
bobhancockg wants to merge 4 commits into
mainfrom
add-demand-gen-campaign-example-v20
Closed

Add Java example for creating a Demand Gen campaign using API V20#845
bobhancockg wants to merge 4 commits into
mainfrom
add-demand-gen-campaign-example-v20

Conversation

@bobhancockg

Copy link
Copy Markdown

This commit introduces a new Java example, AddDemandGenCampaign.java, which demonstrates how to create a Google Ads Demand Gen campaign with a video ad. This example is a port of the equivalent C# version.

The example covers:

  • Creation of a campaign budget.
  • Creation of a Demand Gen campaign (AdvertisingChannelType.DEMAND_GEN) using Target CPA bidding.
  • Creation of an ad group with Demand Gen specific settings (DemandGenAdGroupSettings), including channel controls.
  • Creation of necessary assets: a YouTube video asset and a logo image asset.
  • Creation of a Demand Gen video responsive ad (DemandGenVideoResponsiveAdInfo) linking the assets and including headlines, descriptions, and business name.
  • Bundling all operations into a single MutateGoogleAdsRequest for atomic execution.
  • Command-line arguments for customer ID and video ID.
  • Error handling for GoogleAdsException.

The code uses Google Ads API V20 and follows the established patterns and conventions for Java examples in this repository.

This commit introduces a new Java example, `AddDemandGenCampaign.java`, which demonstrates how to create a Google Ads Demand Gen campaign with a video ad. This example is a port of the equivalent C# version.

The example covers:
- Creation of a campaign budget.
- Creation of a Demand Gen campaign (AdvertisingChannelType.DEMAND_GEN) using Target CPA bidding.
- Creation of an ad group with Demand Gen specific settings (DemandGenAdGroupSettings), including channel controls.
- Creation of necessary assets: a YouTube video asset and a logo image asset.
- Creation of a Demand Gen video responsive ad (DemandGenVideoResponsiveAdInfo) linking the assets and including headlines, descriptions, and business name.
- Bundling all operations into a single `MutateGoogleAdsRequest` for atomic execution.
- Command-line arguments for customer ID and video ID.
- Error handling for `GoogleAdsException`.

The code uses Google Ads API V20 and follows the established patterns and conventions for Java examples in this repository.
…20).

This commit introduces a unit test for the `AddDemandGenCampaign.java` example.
The test (`AddDemandGenCampaignTest.java`) verifies the logic within the `run` method by:
- Mocking `GoogleAdsClient` and `GoogleAdsServiceClient`.
- Capturing the `MutateGoogleAdsRequest` sent to the API.
- Asserting the correctness of all operations (CampaignBudget, Campaign, AdGroup, Assets, AdGroupAd) and their key fields, ensuring they align with the V20 Google Ads API and Demand Gen campaign requirements.
- Verifying that temporary resource IDs are correctly used.

To facilitate testing, I've changed the visibility of the `run` method in `AddDemandGenCampaign.java` from `private` to package-private.
This commit corrects the Google Ads API method call used for submitting mutate operations in the `AddDemandGenCampaign.java` example and its corresponding test `AddDemandGenCampaignTest.java`.

The method `googleAdsServiceClient.mutateGoogleAds()` was incorrectly used. It has been changed to the correct method `googleAdsServiceClient.mutate()`.

This change addresses compilation errors that would occur due to the incorrect method signature and ensures the example aligns with the Google Ads API V20 Java client library.
@@ -0,0 +1,398 @@
// Copyright 2024 Google LLC

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2025

* Creates a Demand Gen campaign, which features a fully automated campaign construction and bidding
* process. It aims to achieve your advertising goals by serving your ads across YouTube, Gmail and
* Discover. For more information about Demand Gen campaigns, see the <a
* href="https://support.google.com/google-ads/answer/13935200">Demand Gen campaigns</a> help page.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This link doesn't seem to work

"https://www.gstatic.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png";

/** Contains command line argument formats for running this example. */
private static class Options extends ArgumentNames {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ArgumentNames class can't be extended since it's final. I assume this was trying to extend CodeSampleParams

*/
public static void main(String[] args) throws IOException {
Options options = new Options();
if (!CodeSampleHelper.parseArguments(args, options, System.out)) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parseArguments is in CodeSampleParams, not CodeSampleHelper.

This should be doing something like Options.parseArguments(args) and if they are not present, it should prompt the user for the args.

.addAllMutateOperations(operations)
.build();

MutateGoogleAdsResponse response = googleAdsServiceClient.mutateGoogleAds(request);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method name is just mutate()

Asset.newBuilder()
.setResourceName(assetResourceName)
.setName("Demand Gen Logo Asset #" + System.currentTimeMillis())
.setType(AssetType.IMAGE)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think setting the type is necessary. And I think her e it should be LOGO

.build();

return MutateOperation.newBuilder()
.setAssetOperation(AssetOperation.newBuilder().setCreate(imageAsset))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing .build()

.setResourceName(assetResourceName)
.setName("Demand Gen Video Asset #" + System.currentTimeMillis())
.setType(AssetType.YOUTUBE_VIDEO)
.setYoutubeVideoAsset(VideoAsset.newBuilder().setYoutubeVideoId(youtubeVideoId))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a YoutubeVideoAsset object, not a VideoAsset object

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And it's missing a .build()

.build();

return MutateOperation.newBuilder()
.setAssetOperation(AssetOperation.newBuilder().setCreate(videoAsset))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing .build()

// The status of the ad group ad is inherited from the ad group status.
// For example, if the ad group is PAUSED, the ad group ad status is also PAUSED.
.setAd(
Ad.newBuilder()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing a bunch of .build()s below

… done so far and provide feedback for Jules to continue.
bobhancockg pushed a commit that referenced this pull request Jun 12, 2025
Addresses a comment in PR #845 regarding missing .build() calls
within the createDemandGenAdOperation method in AddDemandGenCampaign.java.

Added .build() to the following builder chains to ensure correct
construction of the Ad object:
- DemandGenVideoResponsiveAdInfo.newBuilder()
- AdVideoAsset.newBuilder()
- AdImageAsset.newBuilder()
- Multiple instances of AdTextAsset.newBuilder()
@bobhancockg bobhancockg closed this Aug 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants