Java
// Copyright 2020 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // https://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package com.google.ads.googleads.examples.remarketing; import com.beust.jcommander.Parameter; import com.google.ads.googleads.examples.utils.ArgumentNames; import com.google.ads.googleads.examples.utils.CodeSampleParams; import com.google.ads.googleads.lib.GoogleAdsClient; import com.google.ads.googleads.v17.common.Consent; import com.google.ads.googleads.v17.enums.ConsentStatusEnum.ConsentStatus; import com.google.ads.googleads.v17.errors.GoogleAdsError; import com.google.ads.googleads.v17.errors.GoogleAdsException; import com.google.ads.googleads.v17.errors.GoogleAdsFailure; import com.google.ads.googleads.v17.services.CallConversion; import com.google.ads.googleads.v17.services.CallConversionResult; import com.google.ads.googleads.v17.services.ConversionUploadServiceClient; import com.google.ads.googleads.v17.services.CustomVariable; import com.google.ads.googleads.v17.services.UploadCallConversionsRequest; import com.google.ads.googleads.v17.services.UploadCallConversionsResponse; import com.google.ads.googleads.v17.utils.ErrorUtils; import com.google.ads.googleads.v17.utils.ResourceNames; import java.io.FileNotFoundException; import java.io.IOException; /** * Imports offline call conversion values for calls related to the ads in your account. To set up a * conversion action, run the {@link * com.google.ads.googleads.examples.remarketing.AddConversionAction} example. */ public class UploadCallConversion { private static class UploadCallConversionParams extends CodeSampleParams { @Parameter(names = ArgumentNames.CUSTOMER_ID) private long customerId; @Parameter(names = ArgumentNames.CONVERSION_ACTION_ID) private String conversionActionId; @Parameter( names = ArgumentNames.CALLER_ID, description = "The caller ID from which this call was placed. Caller ID is expected to be in E.164" + " format with preceding '+' sign, e.g. \"+18005550100\"") private String callerId; @Parameter( names = ArgumentNames.CALL_START_DATE_TIME, description = "Format is \"yyyy-mm-dd hh:mm:ss+|-hh:mm\", e.g. \"2019-01-01 12:32:45-08:00\"") private String callStartDateTime; @Parameter( names = ArgumentNames.CONVERSION_DATE_TIME, description = "Format is \"yyyy-mm-dd hh:mm:ss+|-hh:mm\", e.g. \"2019-01-01 12:32:45-08:00\"") private String conversionDateTime; @Parameter(names = ArgumentNames.CONVERSION_VALUE) private double conversionValue; // Optional: Specify the conversion custom variable ID and value you want to // associate with the call conversion upload. @Parameter(names = ArgumentNames.CONVERSION_CUSTOM_VARIABLE_ID) private Long conversionCustomVariableId; @Parameter(names = ArgumentNames.CONVERSION_CUSTOM_VARIABLE_VALUE) private String conversionCustomVariableValue; @Parameter(names = ArgumentNames.AD_USER_DATA_CONSENT, required = false) private ConsentStatus adUserDataConsent; } public static void main(String[] args) { UploadCallConversionParams params = new UploadCallConversionParams(); if (!params.parseArguments(args)) { // Either pass the required parameters for this example on the command line, or insert them // into the code here. See the parameter class definition above for descriptions. params.customerId = Long.parseLong("INSERT_CUSTOMER_ID"); params.conversionActionId = "INSERT_CONVERSION_ACTION_ID"; params.callerId = "INSERT_CALLER_ID"; params.callStartDateTime = "INSERT_CALL_START_DATE_TIME"; params.conversionDateTime = "INSERT_CONVERSION_DATE_TIME"; params.conversionValue = Double.parseDouble("INSERT_CONVERSION_VALUE"); // Optionally specify the conversion custom variable ID and value you want to // associate with the call conversion upload. params.conversionCustomVariableId = null; params.conversionCustomVariableValue = null; // Optionally specify the ad user data consent. params.adUserDataConsent = null; } GoogleAdsClient googleAdsClient = null; try { googleAdsClient = GoogleAdsClient.newBuilder().fromPropertiesFile().build(); } catch (FileNotFoundException fnfe) { System.err.printf( "Failed to load GoogleAdsClient configuration from file. Exception: %s%n", fnfe); System.exit(1); } catch (IOException ioe) { System.err.printf("Failed to create GoogleAdsClient. Exception: %s%n", ioe); System.exit(1); } try { new UploadCallConversion() .runExample( googleAdsClient, params.customerId, params.conversionActionId, params.callerId, params.callStartDateTime, params.conversionValue, params.conversionCustomVariableId, params.conversionCustomVariableValue, params.adUserDataConsent); } catch (GoogleAdsException gae) { // GoogleAdsException is the base class for most exceptions thrown by an API request. // Instances of this exception have a message and a GoogleAdsFailure that contains a // collection of GoogleAdsErrors that indicate the underlying causes of the // GoogleAdsException. System.err.printf( "Request ID %s failed due to GoogleAdsException. Underlying errors:%n", gae.getRequestId()); int i = 0; for (GoogleAdsError googleAdsError : gae.getGoogleAdsFailure().getErrorsList()) { System.err.printf(" Error %d: %s%n", i++, googleAdsError); } System.exit(1); } } /** * Runs the example. * * @param googleAdsClient the client. * @param customerId the customer ID. * @param conversionActionId the conversion action ID. * @param callerId the caller ID. * @param callStartDateTime the call start date time * @param conversionValue the value of the conversion in USD. * @param conversionCustomVariableId the ID of the conversion custom variable to associate with * the upload. * @param conversionCustomVariableValue the value of the conversion custom variable to associate * with the upload. * @param adUserDataConsent the ad user data consent. */ private void runExample( GoogleAdsClient googleAdsClient, long customerId, String conversionActionId, String callerId, String callStartDateTime, double conversionValue, Long conversionCustomVariableId, String conversionCustomVariableValue, ConsentStatus adUserDataConsent) { // Create a call conversion by specifying currency as USD. CallConversion.Builder conversionBuilder = CallConversion.newBuilder() .setConversionAction(conversionActionId) .setCallerId(callerId) .setCallStartDateTime(callStartDateTime) .setConversionValue(conversionValue) .setCurrencyCode("USD"); if (conversionCustomVariableId != null && conversionCustomVariableValue != null) { conversionBuilder.addCustomVariables( CustomVariable.newBuilder() .setConversionCustomVariable( ResourceNames.conversionCustomVariable(customerId, conversionCustomVariableId)) .setValue(conversionCustomVariableValue)); } // Sets the consent information, if provided. if (adUserDataConsent != null) { // Specifies whether user consent was obtained for the data you are uploading. See // https://www.google.com/about/company/user-consent-policy for details. conversionBuilder.setConsent(Consent.newBuilder().setAdUserData(adUserDataConsent)); } CallConversion conversion = conversionBuilder.build(); // Uploads the call conversion to the API. try (ConversionUploadServiceClient conversionUploadServiceClient = googleAdsClient.getLatestVersion().createConversionUploadServiceClient()) { // Partial failure MUST be enabled for this request. // NOTE: This request contains a single conversion as a demonstration. However, if you have // multiple conversions to upload, it's best to upload multiple conversions per request // instead of sending a separate request per conversion. See the following for per-request // limits: // https://developers.google.com/google-ads/api/docs/best-practices/quotas#conversion_upload_service UploadCallConversionsResponse response = conversionUploadServiceClient.uploadCallConversions( UploadCallConversionsRequest.newBuilder() .setCustomerId(String.valueOf(customerId)) .setCustomerId(Long.toString(customerId)) .addConversions(conversion) .setPartialFailure(true) .build()); // Prints any partial failure errors returned. if (response.hasPartialFailureError()) { GoogleAdsFailure googleAdsFailure = ErrorUtils.getInstance().getGoogleAdsFailure(response.getPartialFailureError()); googleAdsFailure .getErrorsList() .forEach(e -> System.out.println("Partial failure occurred: " + e.getMessage())); throw new RuntimeException( "Partial failure occurred " + response.getPartialFailureError().getMessage()); } // Prints the result if valid. CallConversionResult result = response.getResults(0); System.out.printf( "Uploaded call conversion that occurred at '%' for caller ID '%' to the conversion" + " action with resource name '%'.%n", result.getCallStartDateTime(), result.getCallerId(), result.getConversionAction()); } } }
#C
// Copyright 2020 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. using CommandLine; using Google.Ads.Gax.Examples; using Google.Ads.GoogleAds.Lib; using Google.Ads.GoogleAds.V17.Common; using Google.Ads.GoogleAds.V17.Errors; using Google.Ads.GoogleAds.V17.Services; using System; using System.Collections.Generic; using static Google.Ads.GoogleAds.V17.Enums.ConsentStatusEnum.Types; namespace Google.Ads.GoogleAds.Examples.V17 { /// <summary> /// This code example imports offline call conversion values for calls related to the /// ads in your account. To set up a conversion action, run the AddConversionAction.cs example. /// </summary> public class UploadCallConversion : ExampleBase { /// <summary> /// Command line options for running the <see cref="UploadCallConversion"/> example. /// </summary> public class Options : OptionsBase { /// <summary> /// The Google Ads customer ID for whom the conversion will be imported. /// </summary> [Option("customerId", Required = true, HelpText = "The Google Ads customer ID for whom the conversion will be imported.")] public long CustomerId { get; set; } /// <summary> /// The conversion action ID. /// </summary> [Option("conversionActionId", Required = true, HelpText = "The conversion action ID.")] public long ConversionActionId { get; set; } /// <summary> /// The caller ID in E.164 format with preceding '+' sign. e.g. "+18005550100". /// </summary> [Option("callerId", Required = true, HelpText = "The caller ID in E.164 format with preceding '+' sign. e.g. '+18005550100'.")] public string CallerId { get; set; } /// <summary> /// The call start time in 'yyyy-mm-dd hh:mm:ss +|-hh:mm' format. /// </summary> [Option("callStartTime", Required = true, HelpText = "The call start time in 'yyyy-mm-dd hh:mm:ss+|-hh:mm' format.")] public string CallStartTime { get; set; } /// <summary> /// The call conversion time. /// </summary> [Option("conversionTime", Required = true, HelpText = "The conversion time in 'yyyy-mm-dd hh:mm:ss+|-hh:mm' format.")] public string ConversionTime { get; set; } /// <summary> /// The conversion value. /// </summary> [Option("conversionValue", Required = true, HelpText = "The conversion value.")] public double ConversionValue { get; set; } /// <summary> /// The ID of the conversion custom variable to associate with the upload. /// </summary> [Option("conversionCustomVariableId", Required = false, HelpText = "The ID of the conversion custom variable to associate with the upload.")] public long? ConversionCustomVariableId { get; set; } /// <summary> /// The value of the conversion custom variable to associate with the upload. /// </summary> [Option("conversionCustomVariableValue", Required = false, HelpText = "The value of the conversion custom variable to associate with the upload.")] public string ConversionCustomVariableValue { get; set; } /// <summary> /// The consent status for ad user data. /// </summary> [Option("adUserDataConsent", Required = false, HelpText = "The consent status for ad user data.")] public ConsentStatus? AdUserDataConsent { get; set; } } /// <summary> /// Main method, to run this code example as a standalone application. /// </summary> /// <param name="args">The command line arguments.</param> public static void Main(string[] args) { Options options = ExampleUtilities.ParseCommandLine<Options>(args); UploadCallConversion codeExample = new UploadCallConversion(); Console.WriteLine(codeExample.Description); codeExample.Run(new GoogleAdsClient(), options.CustomerId, options.ConversionActionId, options.CallerId, options.CallStartTime, options.ConversionTime, options.ConversionValue, options.ConversionCustomVariableId, options.ConversionCustomVariableValue, options.AdUserDataConsent); } /// <summary> /// Returns a description about the code example. /// </summary> public override string Description => "This code example imports offline call conversion values for calls related to the " + "ads in your account. To set up a conversion action, run the AddConversionAction.cs " + "example."; /// <summary> /// Runs the code example. /// </summary> /// <param name="client">The Google Ads client.</param> /// <param name="customerId">The Google Ads customer ID for whom the conversion will /// be imported.</param> /// <param name="callerId">The caller ID in E.164 format with preceding '+' sign. e.g. /// "+18005550100".</param> /// <param name="conversionActionId">The ID of the conversion action</param> /// <param name="callStartTime">The call start time in "yyyy-mm-dd hh:mm:ss+|-hh:mm" /// format.</param> /// <param name="conversionTime">The conversion time in "yyyy-mm-dd hh:mm:ss+|-hh:mm" /// format.</param> /// <param name="conversionValue">The conversion value.</param> /// <param name="conversionCustomVariableId">The ID of the conversion custom variable to /// associate with the upload.</param> /// <param name="conversionCustomVariableValue">The value of the conversion custom variable /// to associate with the upload.</param> /// <param name="adUserDataConsent">The consent status for ad user data.</param> public void Run(GoogleAdsClient client, long customerId, long conversionActionId, string callerId, string callStartTime, string conversionTime, double conversionValue, long? conversionCustomVariableId, string conversionCustomVariableValue, ConsentStatus? adUserDataConsent) { // Get the ConversionUploadService. ConversionUploadServiceClient conversionUploadService = client.GetService(Services.V17.ConversionUploadService); // Create a call conversion by specifying currency as USD. CallConversion callConversion = new CallConversion() { ConversionAction = ResourceNames.ConversionAction(customerId, conversionActionId), CallerId = callerId, CallStartDateTime = callStartTime, ConversionDateTime = conversionTime, ConversionValue = conversionValue, CurrencyCode = "USD", }; if (adUserDataConsent != null) { // Specifies whether user consent was obtained for the data you are uploading. See // https://www.google.com/about/company/user-consent-policy // for details. callConversion.Consent = new Consent() { AdUserData = (ConsentStatus)adUserDataConsent }; } if (conversionCustomVariableId != null && !string.IsNullOrEmpty(conversionCustomVariableValue)) { callConversion.CustomVariables.Add(new CustomVariable() { ConversionCustomVariable = ResourceNames.ConversionCustomVariable( customerId, conversionCustomVariableId.Value), Value = conversionCustomVariableValue }); } UploadCallConversionsRequest request = new UploadCallConversionsRequest() { CustomerId = customerId.ToString(), Conversions = { callConversion }, PartialFailure = true }; try { // Issues a request to upload the call conversion. The partialFailure parameter // is set to true, and validateOnly parameter to false as required by this method // call. // NOTE: This request contains a single conversion as a demonstration. However, if // you have multiple conversions to upload, it's best to upload multiple conversions // per request instead of sending a separate request per conversion. See the // following for per-request limits: // https://developers.google.com/google-ads/api/docs/best-practices/quotas#conversion_upload_service UploadCallConversionsResponse response = conversionUploadService.UploadCallConversions(request); // Since we set partialFailure = true, we can retrieve error messages (if any) from // the operation response. if (response.PartialFailureError != null) { Console.WriteLine("Call conversion upload failed."); // Retrieves the errors from the partial failure and prints them. List<GoogleAdsError> errors = response.PartialFailure.GetErrorsByOperationIndex(0); foreach (GoogleAdsError error in errors) { Console.WriteLine($"Operation failed with error: {error}."); } } else { // Prints the result. CallConversionResult uploadedCallConversion = response.Results[0]; Console.WriteLine($"Uploaded call conversion that occurred at " + $"'{uploadedCallConversion.CallStartDateTime}' for caller ID " + $"'{uploadedCallConversion.CallerId}' to the conversion action with " + $"resource name '{uploadedCallConversion.ConversionAction}'."); } } catch (GoogleAdsException e) { Console.WriteLine("Failure:"); Console.WriteLine($"Message: {e.Message}"); Console.WriteLine($"Failure: {e.Failure}"); Console.WriteLine($"Request ID: {e.RequestId}"); throw; } } } }
PHP
<?php /** * Copyright 2020 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * https://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ namespace Google\Ads\GoogleAds\Examples\Remarketing; require __DIR__ . '/../../vendor/autoload.php'; use GetOpt\GetOpt; use Google\Ads\GoogleAds\Examples\Utils\ArgumentNames; use Google\Ads\GoogleAds\Examples\Utils\ArgumentParser; use Google\Ads\GoogleAds\Lib\OAuth2TokenBuilder; use Google\Ads\GoogleAds\Lib\V17\GoogleAdsClient; use Google\Ads\GoogleAds\Lib\V17\GoogleAdsClientBuilder; use Google\Ads\GoogleAds\Lib\V17\GoogleAdsException; use Google\Ads\GoogleAds\Util\V17\ResourceNames; use Google\Ads\GoogleAds\V17\Common\Consent; use Google\Ads\GoogleAds\V17\Enums\ConsentStatusEnum\ConsentStatus; use Google\Ads\GoogleAds\V17\Errors\GoogleAdsError; use Google\Ads\GoogleAds\V17\Services\CallConversion; use Google\Ads\GoogleAds\V17\Services\CallConversionResult; use Google\Ads\GoogleAds\V17\Services\CustomVariable; use Google\Ads\GoogleAds\V17\Services\UploadCallConversionsRequest; use Google\ApiCore\ApiException; /** * This example imports offline call conversion values for calls related to the ads in your account. * To set up a conversion action, run the AddConversionAction.php example. */ class UploadCallConversion { private const CUSTOMER_ID = 'INSERT_CUSTOMER_ID_HERE'; private const CONVERSION_ACTION_ID = 'INSERT_CONVERSION_ACTION_ID_HERE'; private const CALLER_ID = 'INSERT_CALLER_ID_HERE'; private const CALL_START_DATE_TIME = 'INSERT_CALL_START_DATE_TIME_HERE'; private const CONVERSION_DATE_TIME = 'INSERT_CONVERSION_DATE_TIME_HERE'; private const CONVERSION_VALUE = 'INSERT_CONVERSION_VALUE_HERE'; // Optional: Specify the conversion custom variable ID and value you want to // associate with the call conversion upload. private const CONVERSION_CUSTOM_VARIABLE_ID = null; private const CONVERSION_CUSTOM_VARIABLE_VALUE = null; // Optional: The consent status for ad user data. private const AD_USER_DATA_CONSENT = null; public static function main() { // Either pass the required parameters for this example on the command line, or insert them // into the constants above. $options = (new ArgumentParser())->parseCommandArguments([ ArgumentNames::CUSTOMER_ID => GetOpt::REQUIRED_ARGUMENT, ArgumentNames::CONVERSION_ACTION_ID => GetOpt::REQUIRED_ARGUMENT, ArgumentNames::CALLER_ID => GetOpt::REQUIRED_ARGUMENT, ArgumentNames::CALL_START_DATE_TIME => GetOpt::REQUIRED_ARGUMENT, ArgumentNames::CONVERSION_DATE_TIME => GetOpt::REQUIRED_ARGUMENT, ArgumentNames::CONVERSION_VALUE => GetOpt::REQUIRED_ARGUMENT, ArgumentNames::CONVERSION_CUSTOM_VARIABLE_ID => GetOpt::OPTIONAL_ARGUMENT, ArgumentNames::CONVERSION_CUSTOM_VARIABLE_VALUE => GetOpt::OPTIONAL_ARGUMENT, ArgumentNames::AD_USER_DATA_CONSENT => GetOpt::OPTIONAL_ARGUMENT ]); // Generate a refreshable OAuth2 credential for authentication. $oAuth2Credential = (new OAuth2TokenBuilder())->fromFile()->build(); // Construct a Google Ads client configured from a properties file and the // OAuth2 credentials above. $googleAdsClient = (new GoogleAdsClientBuilder()) ->fromFile() ->withOAuth2Credential($oAuth2Credential) // We set this value to true to show how to use GAPIC v2 source code. You can remove the // below line if you wish to use the old-style source code. Note that in that case, you // probably need to modify some parts of the code below to make it work. // For more information, see // https://developers.devsite.corp.google.com/google-ads/api/docs/client-libs/php/gapic. ->usingGapicV2Source(true) ->build(); try { self::runExample( $googleAdsClient, $options[ArgumentNames::CUSTOMER_ID] ?: self::CUSTOMER_ID, $options[ArgumentNames::CONVERSION_ACTION_ID] ?: self::CONVERSION_ACTION_ID, $options[ArgumentNames::CALLER_ID] ?: self::CALLER_ID, $options[ArgumentNames::CALL_START_DATE_TIME] ?: self::CALL_START_DATE_TIME, $options[ArgumentNames::CONVERSION_DATE_TIME] ?: self::CONVERSION_DATE_TIME, $options[ArgumentNames::CONVERSION_VALUE] ?: self::CONVERSION_VALUE, $options[ArgumentNames::CONVERSION_CUSTOM_VARIABLE_ID] ?: self::CONVERSION_CUSTOM_VARIABLE_ID, $options[ArgumentNames::CONVERSION_CUSTOM_VARIABLE_VALUE] ?: self::CONVERSION_CUSTOM_VARIABLE_VALUE, $options[ArgumentNames::AD_USER_DATA_CONSENT] ? ConsentStatus::value($options[ArgumentNames::AD_USER_DATA_CONSENT]) : self::AD_USER_DATA_CONSENT ); } catch (GoogleAdsException $googleAdsException) { printf( "Request with ID '%s' has failed.%sGoogle Ads failure details:%s", $googleAdsException->getRequestId(), PHP_EOL, PHP_EOL ); foreach ($googleAdsException->getGoogleAdsFailure()->getErrors() as $error) { /** @var GoogleAdsError $error */ printf( "\t%s: %s%s", $error->getErrorCode()->getErrorCode(), $error->getMessage(), PHP_EOL ); } exit(1); } catch (ApiException $apiException) { printf( "ApiException was thrown with message '%s'.%s", $apiException->getMessage(), PHP_EOL ); exit(1); } } /** * Runs the example. * * @param GoogleAdsClient $googleAdsClient the Google Ads API client * @param int $customerId the customer ID * @param int $conversionActionId the ID of the conversion action to upload to * @param string $callerId the caller ID from which this call was placed. Caller ID is expected * to be in E.164 format with preceding '+' sign. e.g. "+18005550100" * @param string $callStartDateTime the date and time at which the call occurred. The format is * "yyyy-mm-dd hh:mm:ss+|-hh:mm", e.g. “2019-01-01 12:32:45-08:00” * @param string $conversionDateTime the date and time of the conversion (should be after the * call time). The format is "yyyy-mm-dd hh:mm:ss+|-hh:mm", e.g. * “2019-01-01 12:32:45-08:00” * @param float $conversionValue the value of the conversion * @param string|null $conversionCustomVariableId the ID of the conversion custom variable to * associate with the upload * @param string|null $conversionCustomVariableValue the value of the conversion custom * variable to associate with the upload * @param int|null $adUserDataConsent the consent status for ad user data */ public static function runExample( GoogleAdsClient $googleAdsClient, int $customerId, int $conversionActionId, string $callerId, string $callStartDateTime, string $conversionDateTime, float $conversionValue, ?string $conversionCustomVariableId, ?string $conversionCustomVariableValue, ?int $adUserDataConsent ) { // Creates a call conversion by specifying currency as USD. $callConversion = new CallConversion([ 'conversion_action' => ResourceNames::forConversionAction($customerId, $conversionActionId), 'caller_id' => $callerId, 'call_start_date_time' => $callStartDateTime, 'conversion_date_time' => $conversionDateTime, 'conversion_value' => $conversionValue, 'currency_code' => 'USD' ]); if (!is_null($conversionCustomVariableId) && !is_null($conversionCustomVariableValue)) { $callConversion->setCustomVariables([new CustomVariable([ 'conversion_custom_variable' => ResourceNames::forConversionCustomVariable( $customerId, $conversionCustomVariableId ), 'value' => $conversionCustomVariableValue ])]); } // Sets the consent information, if provided. if (!empty($adUserDataConsent)) { // Specifies whether user consent was obtained for the data you are uploading. See // https://www.google.com/about/company/user-consent-policy for details. $callConversion->setConsent(new Consent(['ad_user_data' => $adUserDataConsent])); } // Issues a request to upload the call conversion. $conversionUploadServiceClient = $googleAdsClient->getConversionUploadServiceClient(); // NOTE: This request contains a single conversion as a demonstration. However, if you have // multiple conversions to upload, it's best to upload multiple conversions per request // instead of sending a separate request per conversion. See the following for per-request // limits: // https://developers.google.com/google-ads/api/docs/best-practices/quotas#conversion_upload_service $response = $conversionUploadServiceClient->uploadCallConversions( // Partial failure MUST be enabled for this request. UploadCallConversionsRequest::build($customerId, [$callConversion], true) ); // Prints the status message if any partial failure error is returned. // Note: The details of each partial failure error are not printed here, you can refer to // the example HandlePartialFailure.php to learn more. if ($response->hasPartialFailureError()) { printf( "Partial failures occurred: '%s'.%s", $response->getPartialFailureError()->getMessage(), PHP_EOL ); } else { // Prints the result if exists. /** @var CallConversionResult $uploadedCallConversion */ $uploadedCallConversion = $response->getResults()[0]; printf( "Uploaded call conversion that occurred at '%s' for caller ID '%s' to the " . "conversion action with resource name '%s'.%s", $uploadedCallConversion->getCallStartDateTime(), $uploadedCallConversion->getCallerId(), $uploadedCallConversion->getConversionAction(), PHP_EOL ); } } } UploadCallConversion::main();
Python
#!/usr/bin/env python # Copyright 2020 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # https://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Imports offline call conversion values for calls related to your ads. To set up a conversion action, run the add_conversion_action.py example. """ import argparse import sys from google.ads.googleads.client import GoogleAdsClient from google.ads.googleads.errors import GoogleAdsException def main( client, customer_id, conversion_action_id, caller_id, call_start_date_time, conversion_date_time, conversion_value, conversion_custom_variable_id, conversion_custom_variable_value, ad_user_data_consent, ): """Imports offline call conversion values for calls related to your ads. Args: client: An initialized GoogleAdsClient instance. customer_id: The client customer ID string. conversion_action_id: The ID of the conversion action to upload to. caller_id: The caller ID from which this call was placed. Caller ID is expected to be in E.164 format with preceding '+' sign, e.g. '+18005550100'. call_start_date_time: The date and time at which the call occurred. The format is 'yyyy-mm-dd hh:mm:ss+|-hh:mm', e.g. '2021-01-01 12:32:45-08:00'. conversion_date_time: The the date and time of the conversion (should be after the click time). The format is 'yyyy-mm-dd hh:mm:ss+|-hh:mm', e.g. '2021-01-01 12:32:45-08:00'. conversion_value: The conversion value in the desired currency. conversion_custom_variable_id: The ID of the conversion custom variable to associate with the upload. conversion_custom_variable_value: The str value of the conversion custom variable to associate with the upload. ad_user_data_consent: The consent status for ad user data for all members in the job. """ # Get the ConversionUploadService client. conversion_upload_service = client.get_service("ConversionUploadService") # Create a call conversion in USD currency. call_conversion = client.get_type("CallConversion") call_conversion.conversion_action = client.get_service( "ConversionActionService" ).conversion_action_path(customer_id, conversion_action_id) call_conversion.caller_id = caller_id call_conversion.call_start_date_time = call_start_date_time call_conversion.conversion_date_time = conversion_date_time call_conversion.conversion_value = conversion_value call_conversion.currency_code = "USD" if conversion_custom_variable_id and conversion_custom_variable_value: conversion_custom_variable = client.get_type("CustomVariable") conversion_custom_variable.conversion_custom_variable = ( conversion_custom_variable_id ) conversion_custom_variable.value = conversion_custom_variable_value call_conversion.custom_variables.append(conversion_custom_variable) # Specifies whether user consent was obtained for the data you are # uploading. For more details. see: # https://www.google.com/about/company/user-consent-policy if ad_user_data_consent: call_conversion.consent.ad_user_data = client.enums.ConsentStatusEnum[ ad_user_data_consent ] # Issue a request to upload the call conversion. # Partial failure MUST be enabled for this request. request = client.get_type("UploadCallConversionsRequest") request.customer_id = customer_id request.conversions = [call_conversion] request.partial_failure = True # NOTE: This request only uploads a single conversion, but if you have # multiple conversions to upload, it's most efficient to upload them in a # single request. See the following for per-request limits for reference: # https://developers.google.com/google-ads/api/docs/best-practices/quotas#conversion_upload_service upload_call_conversions_response = ( conversion_upload_service.upload_call_conversions(request=request) ) # Print any partial errors returned. if upload_call_conversions_response.partial_failure_error: print( "Partial error occurred: " f"'{upload_call_conversions_response.partial_failure_error.message}'" ) # Print the result if valid. uploaded_call_conversion = upload_call_conversions_response.results[0] if uploaded_call_conversion.call_start_date_time: print( "Uploaded call conversion that occurred at " f"'{uploaded_call_conversion.call_start_date_time}' " f"for caller ID '{uploaded_call_conversion.caller_id}' " "to the conversion action with resource name " f"'{uploaded_call_conversion.conversion_action}'." ) if __name__ == "__main__": parser = argparse.ArgumentParser( description="Imports offline call conversion values for calls related " "to your ads." ) # The following argument(s) should be provided to run the example. parser.add_argument( "-c", "--customer_id", type=str, required=True, help="The Google Ads customer ID.", ) parser.add_argument( "-a", "--conversion_action_id", type=str, required=True, help="The ID of the conversion action to upload to.", ) parser.add_argument( "-i", "--caller_id", type=str, required=True, help="The caller ID from which this call was placed. Caller ID is " "expected to be in E.164 format with preceding '+' sign, " "e.g. '+18005550100'.", ) parser.add_argument( "-s", "--call_start_date_time", type=str, required=True, help="The date and time at which the call occurred. The format is " "'yyyy-mm-dd hh:mm:ss+|-hh:mm', e.g. '2019-01-01 12:32:45-08:00'.", ) parser.add_argument( "-t", "--conversion_date_time", type=str, required=True, help="The date and time of the conversion (should be after the " "click time). The format is 'yyyy-mm-dd hh:mm:ss+|-hh:mm', e.g. " "'2019-01-01 12:32:45-08:00'.", ) parser.add_argument( "-v", "--conversion_value", type=float, required=True, help="The conversion value in the desired currency.", ) parser.add_argument( "-w", "--conversion_custom_variable_id", type=str, help="The ID of the conversion custom variable to associate with the upload.", ) parser.add_argument( "-x", "--conversion_custom_variable_value", type=str, help="The value of the conversion custom variable to associate with the upload.", ) parser.add_argument( "-d", "--ad_user_data_consent", type=str, choices=[e.name for e in googleads_client.enums.ConsentStatusEnum], help=( "The data consent status for ad user data for all members in " "the job." ), ) args = parser.parse_args() # GoogleAdsClient will read the google-ads.yaml configuration file in the # home directory if none is specified. googleads_client = GoogleAdsClient.load_from_storage(version="v17") try: main( googleads_client, args.customer_id, args.conversion_action_id, args.caller_id, args.call_start_date_time, args.conversion_date_time, args.conversion_value, args.conversion_custom_variable_id, args.conversion_custom_variable_value, args.ad_user_data_consent, ) except GoogleAdsException as ex: print( f"Request with ID '{ex.request_id}'' failed with status " f"'{ex.error.code().name}' and includes the following errors:" ) for error in ex.failure.errors: print(f"\tError with message '{error.message}'.") if error.location: for field_path_element in error.location.field_path_elements: print(f"\t\tOn field: {field_path_element.field_name}") sys.exit(1)
Ruby
#!/usr/bin/env ruby # Encoding: utf-8 # # Copyright 2020 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # https://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # # This example imports offline call conversion values for calls related to the # ads in your account. # To set up a conversion action, run the add_conversion_action.rb example. require 'optparse' require 'google/ads/google_ads' require 'date' def upload_call_conversion( customer_id, conversion_action_id, caller_id, call_start_date_time, conversion_date_time, conversion_value, conversion_custom_variable_id, conversion_custom_variable_value, ad_user_data_consent) # GoogleAdsClient will read a config file from # ENV['HOME']/google_ads_config.rb when called without parameters client = Google::Ads::GoogleAds::GoogleAdsClient.new # Create a call conversion by specifying currency as USD. call_conversion = client.resource.call_conversion do |c| c.conversion_action = client.path.conversion_action( customer_id, conversion_action_id) c.caller_id = caller_id c.call_start_date_time = call_start_date_time c.conversion_date_time = conversion_date_time c.conversion_value = conversion_value c.currency_code = "USD" if conversion_custom_variable_id && conversion_custom_variable_value c.custom_variables << client.resource.custom_variable do |cv| cv.conversion_custom_variable = client.path.conversion_custom_variable( customer_id, conversion_custom_variable_id) cv.value = conversion_custom_variable_value end end unless ad_user_data_consent.nil? c.consent = client.resource.consent do |c| # Specifies whether user consent was obtained for the data you are # uploading. For more details, see: # https://www.google.com/about/company/user-consent-policy c.ad_user_data = ad_user_data_consent end end end # Issues a request to upload the call conversion. response = client.service.conversion_upload.upload_call_conversions( customer_id: customer_id, # NOTE: This request only uploads a single conversion, but if you have # multiple conversions to upload, it's most efficient to upload them in a # single request. See the following for per-request limits for reference: # https://developers.google.com/google-ads/api/docs/best-practices/quotas#conversion_upload_service conversions: [call_conversion], partial_failure: true ) # Prints errors if any partial failure error is returned. if response.partial_failure_error failures = client.decode_partial_failure_error(response.partial_failure_error) failures.each do |failure| failure.errors.each do |error| human_readable_error_path = error .location .field_path_elements .map { |location_info| if location_info.index "#{location_info.field_name}[#{location_info.index}]" else "#{location_info.field_name}" end }.join(" > ") errmsg = "error occured while adding operations " \ "#{human_readable_error_path}" \ " with value: #{error.trigger.string_value}" \ " because #{error.message.downcase}" puts errmsg end end else # Print the result if valid. uploaded_call_conversion = response.results.first puts "Uploaded call conversion that occurred at " \ "#{uploaded_call_conversion.call_start_date_time} " \ "for caller ID " \ "#{uploaded_call_conversion.caller_id} " \ "to the conversion action with resource name " \ "#{uploaded_call_conversion.conversion_action}" end end if __FILE__ == $0 options = {} # The following parameter(s) should be provided to run the example. You can # either specify these by changing the INSERT_XXX_ID_HERE values below, or on # the command line. # # Parameters passed on the command line will override any parameters set in # code. # # Running the example with -h will print the command line usage. options[:customer_id] = 'INSERT_CUSTOMER_ID_HERE' # Optional: Specify the conversion custom variable ID and value you want to # associate with the call conversion upload. options[:conversion_custom_variable_id] = nil; options[:conversion_custom_variable_value] = nil; options[:ad_user_data_consent] = nil; OptionParser.new do |opts| opts.banner = sprintf('Usage: %s [options]', File.basename(__FILE__)) opts.separator '' opts.separator 'Options:' opts.on('-C', '--customer-id CUSTOMER-ID', String, 'Customer ID') do |v| options[:customer_id] = v end opts.on('-R', '--conversion-action-id CONVERSION-ACTION-ID', String, 'Conversion Action ID') do |v| options[:conversion_action_id] = v end opts.on('-L', '--caller-id CALLER-ID', String, 'The caller ID from which this call was placed. Caller ID is expected to be in E.164 format with preceding "+" sign, e.g. "+18005550100"') do |v| options[:caller_id] = v end opts.on('-S', '--call-start-date-time CALL-START-DATE-TIME', String, 'Call Start Date Time') do |v| options[:call_start_date_time] = v end opts.on('-D', '--conversion-date-time CONVERSION-DATE-TIME', String, 'Conversion Date Time') do |v| options[:conversion_date_time] = v end opts.on('-V', '--conversion-value CONVERSION-VALUE', String, 'Conversion Value') do |v| options[:conversion_value] = v.to_f end opts.on('-w', '--conversion-custom-variable-id CONVERSION-CUSTOM-VARIABLE-ID', \ String, '(Optional) The ID of the conversion custom variable to ' \ 'associate with the upload') do |v| options[:conversion_custom_variable_id] = v end opts.on('-x', '--conversion-custom-variable-value CONVERSION-CUSTOM-VARIABLE-VALUE', \ String, '(Optional) The value of the conversion custom ' \ 'variable to associate with the upload') do |v| options[:conversion_custom_variable_value] = v end opts.on('-d', '--ad-user-data-consent AD-USER-DATA_CONSENT', \ String, '(Optional) The data consent status for ad user data for all members in the job.' \ 'e.g. UNKNOWN, GRANTED, DENIED') do |v| options[:ad_user_data_consent] = v end opts.separator '' opts.separator 'Help:' opts.on_tail('-h', '--help', 'Show this message') do puts opts exit end end.parse! begin upload_call_conversion( options.fetch(:customer_id).tr("-", ""), options.fetch(:conversion_action_id), options.fetch(:caller_id), options.fetch(:call_start_date_time), options.fetch(:conversion_date_time), options.fetch(:conversion_value), options[:conversion_custom_variable_id], options[:conversion_custom_variable_value], options[:ad_user_data_consent], ) rescue Google::Ads::GoogleAds::Errors::GoogleAdsError => e e.failure.errors.each do |error| STDERR.printf("Error with message: %s\n", error.message) if error.location error.location.field_path_elements.each do |field_path_element| STDERR.printf("\tOn field: %s\n", field_path_element.field_name) end end error.error_code.to_h.each do |k, v| next if v == :UNSPECIFIED STDERR.printf("\tType: %s\n\tCode: %s\n", k, v) end end raise end end
Perl
#!/usr/bin/perl -w # # Copyright 2020, Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # # This example imports offline call conversion values for calls related to the # ads in your account. # To set up a conversion action, run the add_conversion_action.pl example. use strict; use warnings; use utf8; use FindBin qw($Bin); use lib "$Bin/../../lib"; use Google::Ads::GoogleAds::Client; use Google::Ads::GoogleAds::Utils::GoogleAdsHelper; use Google::Ads::GoogleAds::V17::Common::Consent; use Google::Ads::GoogleAds::V17::Services::ConversionUploadService::CallConversion; use Google::Ads::GoogleAds::V17::Services::ConversionUploadService::CustomVariable; use Google::Ads::GoogleAds::V17::Utils::ResourceNames; use Getopt::Long qw(:config auto_help); use Pod::Usage; use Cwd qw(abs_path); # The following parameter(s) should be provided to run the example. You can # either specify these by changing the INSERT_XXX_ID_HERE values below, or on # the command line. # # Parameters passed on the command line will override any parameters set in # code. # # Running the example with -h will print the command line usage. my $customer_id = "INSERT_CUSTOMER_ID_HERE"; my $conversion_action_id = "INSERT_CONVERSION_ACTION_ID_HERE"; my $caller_id = "INSERT_CALLER_ID_HERE"; my $call_start_date_time = "INSERT_CALL_START_DATE_TIME_HERE"; my $conversion_date_time = "INSERT_CONVERSION_DATE_TIME_HERE"; my $conversion_value = "INSERT_CONVERSION_VALUE_HERE"; # Optional: Specify the conversion custom variable ID and value you want to # associate with the call conversion upload. my $conversion_custom_variable_id = undef; my $conversion_custom_variable_value = undef; # Optional: Specify the ad user data consent for the call. my $ad_user_data_consent = undef; sub upload_call_conversion { my ( $api_client, $customer_id, $conversion_action_id, $caller_id, $call_start_date_time, $conversion_date_time, $conversion_value, $conversion_custom_variable_id, $conversion_custom_variable_value, $ad_user_data_consent ) = @_; # Create a call conversion by specifying currency as USD. my $call_conversion = Google::Ads::GoogleAds::V17::Services::ConversionUploadService::CallConversion ->new({ conversionAction => Google::Ads::GoogleAds::V17::Utils::ResourceNames::conversion_action( $customer_id, $conversion_action_id ), callerId => $caller_id, callStartDateTime => $call_start_date_time, conversionDateTime => $conversion_date_time, conversionValue => $conversion_value, currencyCode => "USD" }); if ($conversion_custom_variable_id && $conversion_custom_variable_value) { $call_conversion->{customVariables} = [ Google::Ads::GoogleAds::V17::Services::ConversionUploadService::CustomVariable ->new({ conversionCustomVariable => Google::Ads::GoogleAds::V17::Utils::ResourceNames::conversion_custom_variable( $customer_id, $conversion_custom_variable_id ), value => $conversion_custom_variable_value })]; } # Set the consent information, if provided. if ($ad_user_data_consent) { # Specify whether user consent was obtained for the data you are uploading. # See https://www.google.com/about/company/user-consent-policy for details. $call_conversion->{consent} = Google::Ads::GoogleAds::V17::Common::Consent->new({ adUserData => $ad_user_data_consent }); } # Issue a request to upload the call conversion. # NOTE: This request contains a single conversion as a demonstration. # However, if you have multiple conversions to upload, it's best to # upload multiple conversions per request instead of sending a separate # request per conversion. See the following for per-request limits: # https://developers.google.com/google-ads/api/docs/best-practices/quotas#conversion_upload_service my $upload_call_conversions_response = $api_client->ConversionUploadService()->upload_call_conversions({ customerId => $customer_id, conversions => [$call_conversion], partialFailure => "true" }); # Print any partial errors returned. if ($upload_call_conversions_response->{partialFailureError}) { printf "Partial error encountered: '%s'.\n", $upload_call_conversions_response->{partialFailureError}{message}; } # Print the result if valid. my $uploaded_call_conversion = $upload_call_conversions_response->{results}[0]; if (%$uploaded_call_conversion) { printf "Uploaded call conversion that occurred at '%s' " . "for caller ID '%s' to the conversion action with resource name '%s'.\n", $uploaded_call_conversion->{callStartDateTime}, $uploaded_call_conversion->{callerId}, $uploaded_call_conversion->{conversionAction}; } return 1; } # Don't run the example if the file is being included. if (abs_path($0) ne abs_path(__FILE__)) { return 1; } # Get Google Ads Client, credentials will be read from ~/googleads.properties. my $api_client = Google::Ads::GoogleAds::Client->new(); # By default examples are set to die on any server returned fault. $api_client->set_die_on_faults(1); # Parameters passed on the command line will override any parameters set in code. GetOptions( "customer_id=s" => \$customer_id, "conversion_action_id=i" => \$conversion_action_id, "caller_id=s" => \$caller_id, "call_start_date_time=s" => \$call_start_date_time, "conversion_date_time=s" => \$conversion_date_time, "conversion_value=f" => \$conversion_value, "conversion_custom_variable_id=s" => \$conversion_custom_variable_id, "conversion_custom_variable_value=s" => \$conversion_custom_variable_value, "ad_user_data_consent=s" => \$ad_user_data_consent ); # Print the help message if the parameters are not initialized in the code nor # in the command line. pod2usage(2) if not check_params($customer_id, $conversion_action_id, $caller_id, $call_start_date_time, $conversion_date_time, $conversion_value); # Call the example. upload_call_conversion( $api_client, $customer_id =~ s/-//gr, $conversion_action_id, $caller_id, $call_start_date_time, $conversion_date_time, $conversion_value, $conversion_custom_variable_id, $conversion_custom_variable_value, $ad_user_data_consent ); =pod =head1 NAME upload_call_conversion =head1 DESCRIPTION This example imports offline call conversion values for calls related to the ads in your account. To set up a conversion action, run the add_conversion_action.pl example. =head1 SYNOPSIS upload_call_conversion.pl [options] -help Show the help message. -customer_id The Google Ads customer ID. -conversion_action_id The ID of the conversion action to upload to. -caller_id The caller ID from which this call was placed. Caller ID is expected to be in E.164 format with preceding '+' sign. e.g. "+18005550100". -call_start_date_time The date and time at which the call occurred. The format is "yyyy-mm-dd hh:mm:ss+|-hh:mm", e.g. "2019-01-01 12:32:45-08:00". -conversion_date_time The date and time of the conversion (should be after the call time). The format is "yyyy-mm-dd hh:mm:ss+|-hh:mm", e.g. "2019-01-01 12:32:45-08:00". -conversion_value The value of the conversion. -conversion_custom_variable_id [optional] The ID of the conversion custom variable to associate with the upload. -conversion_custom_variable_value [optional] The value of the conversion custom variable to associate with the upload. -ad_user_data_consent [optional] The ad user data consent. =cut