예제 #1
0
        /// <summary>
        /// Runs the code example.
        /// </summary>
        /// <param name="client">The Google Ads client.</param>
        /// <param name="customerId">The Google Ads customer ID for the conversion action is
        /// added.</param>
        /// <param name="conversionActionId">ID of the conversion action for which adjustments are
        /// uploaded.</param>
        /// <param name="adjustmentType">The type of adjustment.</param>
        /// <param name="conversionDateTime">The original conversion time.</param>
        /// <param name="gclid">The Google Click ID for which adjustments are uploaded.</param>
        /// <param name="adjustmentDateTime">The adjustment date and time.</param>
        /// <param name="restatementValue">The restatement value.</param>
        public void Run(GoogleAdsClient client, long customerId, long conversionActionId,
                        string gclid, string conversionDateTime, string adjustmentDateTime,
                        ConversionAdjustmentType adjustmentType,
                        double?restatementValue)
        {
            // Get the ConversionAdjustmentUploadService.
            ConversionAdjustmentUploadServiceClient conversionAdjustmentUploadService =
                client.GetService(Services.V5.ConversionAdjustmentUploadService);

            // Associate conversion adjustments with the existing conversion action.
            // The GCLID should have been uploaded before with a conversion.
            ConversionAdjustment conversionAdjustment = new ConversionAdjustment()
            {
                ConversionAction  = ResourceNames.ConversionAction(customerId, conversionActionId),
                AdjustmentType    = adjustmentType,
                GclidDateTimePair = new GclidDateTimePair()
                {
                    Gclid = gclid,
                    ConversionDateTime = conversionDateTime,
                },
                AdjustmentDateTime = adjustmentDateTime,
            };

            // Set adjusted value for adjustment type RESTATEMENT.
            if (adjustmentType == ConversionAdjustmentType.Restatement)
            {
                conversionAdjustment.RestatementValue = new RestatementValue()
                {
                    AdjustedValue = restatementValue.Value
                };
            }

            try
            {
                // Issue a request to upload the conversion adjustment.
                UploadConversionAdjustmentsResponse response =
                    conversionAdjustmentUploadService.UploadConversionAdjustments(
                        new UploadConversionAdjustmentsRequest()
                {
                    CustomerId            = customerId.ToString(),
                    ConversionAdjustments = { conversionAdjustment },
                    PartialFailure        = true,
                    ValidateOnly          = false
                });

                ConversionAdjustmentResult result = response.Results[0];
                // Print the result.
                Console.WriteLine($"Uploaded conversion adjustment value of" +
                                  $" '{result.ConversionAction}' for Google Click ID " +
                                  $"'{result.GclidDateTimePair.Gclid}'");
            }
            catch (GoogleAdsException e)
            {
                Console.WriteLine("Failure:");
                Console.WriteLine($"Message: {e.Message}");
                Console.WriteLine($"Failure: {e.Failure}");
                Console.WriteLine($"Request ID: {e.RequestId}");
                throw;
            }
        }
        /// <summary>
        /// Runs the code example.
        /// </summary>
        /// <param name="client">The Google Ads client.</param>
        /// <param name="customerId">The Google Ads customer ID for which the conversion
        /// enhancement is uploaded.</param>
        /// <param name="conversionActionId">ID of the conversion action for which adjustments are
        /// uploaded.</param>
        /// <param name="orderId">The unique order ID (transaction ID) of the conversion.</param>
        /// <param name="conversionDateTime">The date time at which the conversion with the
        /// specified order ID occurred.</param>
        /// <param name="userAgent">The HTTP user agent of the conversion.</param>
        /// <param name="restatementValue">The restatement value.</param>
        /// <param name="restatementCurrencyCode">The currency of the restatement value.</param>
        // [START upload_conversion_enhancement]
        public void Run(GoogleAdsClient client, long customerId, long conversionActionId,
                        string orderId, string conversionDateTime, string userAgent, double?restatementValue,
                        string restatementCurrencyCode)
        {
            // Get the ConversionAdjustmentUploadService.
            ConversionAdjustmentUploadServiceClient conversionAdjustmentUploadService =
                client.GetService(Services.V10.ConversionAdjustmentUploadService);

            // [START create_adjustment]
            // Creates the enhancement adjustment.
            ConversionAdjustment enhancement = new ConversionAdjustment()
            {
                ConversionAction = ResourceNames.ConversionAction(customerId, conversionActionId),
                AdjustmentType   = ConversionAdjustmentType.Enhancement,

                // Enhancements MUST use order ID instead of GCLID date/time pair.
                OrderId = orderId
            };

            // Sets the conversion date and time if provided. Providing this value is optional but
            // recommended.
            if (string.IsNullOrEmpty(conversionDateTime))
            {
                enhancement.GclidDateTimePair = new GclidDateTimePair()
                {
                    ConversionDateTime = conversionDateTime
                };
            }

            // Adds user identifiers, hashing where required.

            // Creates a user identifier using sample values for the user address.
            UserIdentifier addressIdentifier = new UserIdentifier()
            {
                AddressInfo = new OfflineUserAddressInfo()
                {
                    HashedFirstName     = NormalizeAndHash("Joanna"),
                    HashedLastName      = NormalizeAndHash("Smith"),
                    HashedStreetAddress = NormalizeAndHash("1600 Amphitheatre Pkwy"),
                    City        = "Mountain View",
                    State       = "CA",
                    PostalCode  = "94043",
                    CountryCode = "US"
                },
                // Optional: Specifies the user identifier source.
                UserIdentifierSource = UserIdentifierSource.FirstParty
            };

            // Creates a user identifier using the hashed email address.
            UserIdentifier emailIdentifier = new UserIdentifier()
            {
                UserIdentifierSource = UserIdentifierSource.FirstParty,
                // Uses the normalize and hash method specifically for email addresses.
                HashedEmail = NormalizeAndHashEmailAddress("*****@*****.**")
            };

            // Adds the user identifiers to the enhancement adjustment.
            enhancement.UserIdentifiers.AddRange(new[] { addressIdentifier, emailIdentifier });

            // Sets optional fields where a value was provided.
            if (!string.IsNullOrEmpty(userAgent))
            {
                // Sets the user agent. This should match the user agent of the request that
                // sent the original conversion so the conversion and its enhancement are either
                // both attributed as same-device or both attributed as cross-device.
                enhancement.UserAgent = userAgent;
            }

            if (restatementValue != null)
            {
                enhancement.RestatementValue = new RestatementValue()
                {
                    // Sets the new value of the conversion.
                    AdjustedValue = restatementValue.Value
                };
                // Sets the currency of the new value, if provided. Otherwise, the default currency
                // from the conversion action is used, and if that is not set then the account
                // currency is used.
                if (restatementCurrencyCode != null)
                {
                    enhancement.RestatementValue.CurrencyCode = restatementCurrencyCode;
                }
            }
            // [END create_adjustment]
            try
            {
                // Uploads the enhancement adjustment. Partial failure should always be set to true.
                UploadConversionAdjustmentsResponse response =
                    conversionAdjustmentUploadService.UploadConversionAdjustments(
                        new UploadConversionAdjustmentsRequest()
                {
                    CustomerId            = customerId.ToString(),
                    ConversionAdjustments = { enhancement },
                    // Enables partial failure (must be true).
                    PartialFailure = 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.cs to learn more.
                if (response.PartialFailureError != null)
                {
                    // Extracts the partial failure from the response status.
                    GoogleAdsFailure partialFailure = response.PartialFailure;
                    Console.WriteLine($"{partialFailure.Errors.Count} partial failure error(s) " +
                                      $"occurred");
                }
                else
                {
                    // Prints the result.
                    ConversionAdjustmentResult result = response.Results[0];
                    Console.WriteLine($"Uploaded conversion adjustment of " +
                                      $"'{result.ConversionAction}' for order ID '{result.OrderId}'.");
                }
            }
            catch (GoogleAdsException e)
            {
                Console.WriteLine("Failure:");
                Console.WriteLine($"Message: {e.Message}");
                Console.WriteLine($"Failure: {e.Failure}");
                Console.WriteLine($"Request ID: {e.RequestId}");
                throw;
            }
        }