Exemple #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">The conversion action ID.</param>
        /// <param name="conversionTime">The conversion time.</param>
        /// <param name="gclid">The click ID.</param>
        /// <param name="conversionValue">The convsersion value.</param>
        public void Run(GoogleAdsClient client, long customerId, long conversionActionId,
                        string gclid, string conversionTime, double conversionValue)
        {
            // Get the ConversionActionService.
            ConversionUploadServiceClient conversionUploadService =
                client.GetService(Services.V5.ConversionUploadService);

            // Creates a click conversion by specifying currency as USD.
            ClickConversion clickConversion = new ClickConversion()
            {
                ConversionAction   = ResourceNames.ConversionAction(customerId, conversionActionId),
                Gclid              = gclid,
                ConversionValue    = conversionValue,
                ConversionDateTime = conversionTime,
                CurrencyCode       = "USD"
            };

            try
            {
                // Issues a request to upload the click conversion.
                UploadClickConversionsResponse response =
                    conversionUploadService.UploadClickConversions(
                        new UploadClickConversionsRequest()
                {
                    CustomerId     = customerId.ToString(),
                    Conversions    = { clickConversion },
                    PartialFailure = true,
                    ValidateOnly   = false
                });

                // Prints the result.
                ClickConversionResult uploadedClickConversion = response.Results[0];
                Console.WriteLine($"Uploaded conversion that occurred at " +
                                  $"'{uploadedClickConversion.ConversionDateTime}' from Google " +
                                  $"Click ID '{uploadedClickConversion.Gclid}' to " +
                                  $"'{uploadedClickConversion.ConversionAction}'.");
            }
            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 the conversion action is
        /// added.</param>
        /// <param name="conversionActionId">The conversion action ID.</param>
        /// <param name="conversionTime">The conversion time in "yyyy-mm-dd hh:mm:ss+|-hh:mm"
        /// format.</param>
        /// <param name="gclid">The GCLID for the conversion. If set, <code>gbraid</code> and
        /// <code>wbraid</code> must be null.</param>
        /// <param name="gbraid">The GBRAID for the iOS app conversion. If set, <code>gclid</code>
        /// and <code>wbraid</code> must be null.</param>
        /// <param name="wbraid">The WBRAID for the iOS web conversion. If set, <code>gclid</code>
        /// and <code>gbraid</code> must be null.</param>
        /// <param name="conversionValue">The convsersion value.</param>
        // [START upload_offline_conversion]
        public void Run(GoogleAdsClient client, long customerId, long conversionActionId,
                        string gclid, string gbraid, string wbraid, string conversionTime,
                        double conversionValue)
        {
            // Get the ConversionActionService.
            ConversionUploadServiceClient conversionUploadService =
                client.GetService(Services.V10.ConversionUploadService);

            // Creates a click conversion by specifying currency as USD.
            ClickConversion clickConversion = new ClickConversion()
            {
                ConversionAction   = ResourceNames.ConversionAction(customerId, conversionActionId),
                ConversionValue    = conversionValue,
                ConversionDateTime = conversionTime,
                CurrencyCode       = "USD"
            };

            // Verifies that exactly one of gclid, gbraid, and wbraid is specified, as required.
            // See https://developers.google.com/google-ads/api/docs/conversions/upload-clicks
            // for details.
            string[] ids     = { gclid, gbraid, wbraid };
            int      idCount = ids.Where(id => !string.IsNullOrEmpty(id)).Count();

            if (idCount != 1)
            {
                throw new ArgumentException($"Exactly 1 of gclid, gbraid, or wbraid is " +
                                            $"required, but {idCount} ID values were provided");
            }

            // Sets the single specified ID field.
            if (!string.IsNullOrEmpty(gclid))
            {
                clickConversion.Gclid = gclid;
            }
            else if (!string.IsNullOrEmpty(wbraid))
            {
                clickConversion.Wbraid = wbraid;
            }
            else if (!string.IsNullOrEmpty(gbraid))
            {
                clickConversion.Gbraid = gbraid;
            }

            try
            {
                // Issues a request to upload the click conversion.
                UploadClickConversionsResponse response =
                    conversionUploadService.UploadClickConversions(
                        new UploadClickConversionsRequest()
                {
                    CustomerId     = customerId.ToString(),
                    Conversions    = { clickConversion },
                    PartialFailure = true,
                    ValidateOnly   = false
                });

                // Prints the result.
                ClickConversionResult uploadedClickConversion = response.Results[0];
                Console.WriteLine($"Uploaded conversion that occurred at " +
                                  $"'{uploadedClickConversion.ConversionDateTime}' from Google " +
                                  $"Click ID '{uploadedClickConversion.Gclid}' to " +
                                  $"'{uploadedClickConversion.ConversionAction}'.");
            }
            catch (GoogleAdsException e)
            {
                Console.WriteLine("Failure:");
                Console.WriteLine($"Message: {e.Message}");
                Console.WriteLine($"Failure: {e.Failure}");
                Console.WriteLine($"Request ID: {e.RequestId}");
                throw;
            }
        }
Exemple #3
0
        /// <summary>
        /// Runs the code example.
        /// </summary>
        /// <param name="client">The Google Ads client.</param>
        /// <param name="customerId">The Google Ads customer ID for which conversions are uploaded.
        /// </param>
        /// <param name="conversionActionId">ID of the conversion action for which conversions are
        /// uploaded.</param>
        /// <param name="emailAddress">The email address.</param>
        /// <param name="conversionDateTime">The date time at which the conversion occurred.</param>
        /// <param name="conversionValue">The conversion value.</param>
        /// <param name="orderId">The unique order ID (transaction ID) of the conversion.</param>
        // [START upload_conversion_with_identifiers]
        public void Run(GoogleAdsClient client, long customerId, long conversionActionId,
                        string emailAddress, string conversionDateTime, double conversionValue,
                        string orderId)
        {
            // Get the ConversionUploadService.
            ConversionUploadServiceClient conversionUploadService =
                client.GetService(Services.V10.ConversionUploadService);

            // [START create_conversion]
            // Gets the conversion action resource name.
            string conversionActionResourceName =
                ResourceNames.ConversionAction(customerId, conversionActionId);

            // Creates a builder for constructing the click conversion.
            ClickConversion clickConversion = new ClickConversion()
            {
                ConversionAction   = conversionActionResourceName,
                ConversionDateTime = conversionDateTime,
                ConversionValue    = conversionValue,
                CurrencyCode       = "USD"
            };

            // Sets the order ID if provided.
            if (!string.IsNullOrEmpty(orderId))
            {
                clickConversion.OrderId = orderId;
            }

            // Optional: Specifies the user identifier source.
            clickConversion.UserIdentifiers.Add(new UserIdentifier()
            {
                // Creates a user identifier using the hashed email address, using the normalize
                // and hash method specifically for email addresses.
                // If using a phone number, use the NormalizeAndHash(String) method instead.
                HashedEmail = NormalizeAndHashEmailAddress(emailAddress),
                // Optional: Specifies the user identifier source.
                UserIdentifierSource = UserIdentifierSource.FirstParty
            });
            // [END create_conversion]

            try
            {
                // Uploads the click conversion. Partial failure should always be set to true.
                UploadClickConversionsResponse response =
                    conversionUploadService.UploadClickConversions(
                        new UploadClickConversionsRequest()
                {
                    CustomerId  = customerId.ToString(),
                    Conversions = { clickConversion },
                    // Enables partial failure (must be true).
                    PartialFailure = true
                });

                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
                {
                    ClickConversionResult result = response.Results[0];
                    // Prints the result.
                    Console.WriteLine($"Uploaded conversion that occurred at" +
                                      $" {result.ConversionDateTime} to {result.ConversionAction}.");
                }
            }
            catch (GoogleAdsException e)
            {
                Console.WriteLine("Failure:");
                Console.WriteLine($"Message: {e.Message}");
                Console.WriteLine($"Failure: {e.Failure}");
                Console.WriteLine($"Request ID: {e.RequestId}");
                throw;
            }
        }