Esempio n. 1
0
        /// <summary>
        /// Runs the code example.
        /// </summary>
        /// <param name="user">The AdWords user.</param>
        public void Run(AdWordsUser user)
        {
            // Get the UserListService.
            AdwordsUserListService userListService =
                (AdwordsUserListService)user.GetService(AdWordsService.v201710.AdwordsUserListService);

            // Get the ConversionTrackerService.
            ConversionTrackerService conversionTrackerService =
                (ConversionTrackerService)user.GetService(AdWordsService.v201710.
                                                          ConversionTrackerService);

            BasicUserList userList = new BasicUserList();

            userList.name               = "Mars cruise customers #" + ExampleUtilities.GetRandomString();
            userList.description        = "A list of mars cruise customers in the last year.";
            userList.status             = UserListMembershipStatus.OPEN;
            userList.membershipLifeSpan = 365;

            UserListConversionType conversionType = new UserListConversionType();

            conversionType.name      = userList.name;
            userList.conversionTypes = new UserListConversionType[] { conversionType };

            // Optional: Set the user list status.
            userList.status = UserListMembershipStatus.OPEN;

            // Create the operation.
            UserListOperation operation = new UserListOperation();

            operation.operand   = userList;
            operation.@operator = Operator.ADD;

            try {
                // Add the user list.
                UserListReturnValue retval = userListService.mutate(new UserListOperation[] { operation });

                UserList[] userLists = null;
                if (retval != null && retval.value != null)
                {
                    userLists = retval.value;
                    // Get all conversion snippets
                    List <string> conversionIds = new List <string>();
                    foreach (BasicUserList newUserList in userLists)
                    {
                        if (newUserList.conversionTypes != null)
                        {
                            foreach (UserListConversionType newConversionType in newUserList.conversionTypes)
                            {
                                conversionIds.Add(newConversionType.id.ToString());
                            }
                        }
                    }

                    Dictionary <long, ConversionTracker> conversionsMap =
                        new Dictionary <long, ConversionTracker>();

                    if (conversionIds.Count > 0)
                    {
                        // Create the selector.
                        Selector selector = new Selector()
                        {
                            fields     = new string[] { ConversionTracker.Fields.Id },
                            predicates = new Predicate[] {
                                Predicate.In(ConversionTracker.Fields.Id, conversionIds)
                            }
                        };

                        // Get all conversion trackers.
                        ConversionTrackerPage page = conversionTrackerService.get(selector);

                        if (page != null && page.entries != null)
                        {
                            foreach (ConversionTracker tracker in page.entries)
                            {
                                conversionsMap[tracker.id] = tracker;
                            }
                        }
                    }

                    // Display the results.
                    foreach (BasicUserList newUserList in userLists)
                    {
                        Console.WriteLine("User list with name '{0}' and id '{1}' was added.",
                                          newUserList.name, newUserList.id);

                        // Display user list associated conversion code snippets.
                        if (newUserList.conversionTypes != null)
                        {
                            foreach (UserListConversionType userListConversionType in
                                     newUserList.conversionTypes)
                            {
                                if (conversionsMap.ContainsKey(userListConversionType.id))
                                {
                                    AdWordsConversionTracker conversionTracker =
                                        (AdWordsConversionTracker)conversionsMap[userListConversionType.id];
                                    Console.WriteLine("Conversion type code snippet associated to the list:\n{0}\n",
                                                      conversionTracker.snippet);
                                }
                                else
                                {
                                    throw new Exception("Failed to associate conversion type code snippet.");
                                }
                            }
                        }
                    }
                }
                else
                {
                    Console.WriteLine("No user lists (a.k.a. audiences) were added.");
                }
                conversionTrackerService.Close();
            } catch (Exception e) {
                throw new System.ApplicationException("Failed to add user lists (a.k.a. audiences).", e);
            }
        }
        /// <summary>
        /// Runs the code example.
        /// </summary>
        /// <param name="user">The AdWords user.</param>
        public void Run(AdWordsUser user)
        {
            // Get the ConversionTrackerService.
            ConversionTrackerService conversionTrackerService =
                (ConversionTrackerService)user.GetService(AdWordsService.v201710.
                                                          ConversionTrackerService);

            List <ConversionTracker> conversionTrackers = new List <ConversionTracker>();

            // Create an Adwords conversion tracker.
            AdWordsConversionTracker adWordsConversionTracker = new AdWordsConversionTracker();

            adWordsConversionTracker.name = "Earth to Mars Cruises Conversion #" +
                                            ExampleUtilities.GetRandomString();
            adWordsConversionTracker.category = ConversionTrackerCategory.DEFAULT;

            // Set optional fields.
            adWordsConversionTracker.status = ConversionTrackerStatus.ENABLED;
            adWordsConversionTracker.viewthroughLookbackWindow    = 15;
            adWordsConversionTracker.defaultRevenueValue          = 23.41;
            adWordsConversionTracker.alwaysUseDefaultRevenueValue = true;
            conversionTrackers.Add(adWordsConversionTracker);

            // Create an upload conversion for offline conversion imports.
            UploadConversion uploadConversion = new UploadConversion();

            // Set an appropriate category. This field is optional, and will be set to
            // DEFAULT if not mentioned.
            uploadConversion.category = ConversionTrackerCategory.LEAD;
            uploadConversion.name     = "Upload Conversion #" + ExampleUtilities.GetRandomString();
            uploadConversion.viewthroughLookbackWindow = 30;
            uploadConversion.ctcLookbackWindow         = 90;

            // Optional: Set the default currency code to use for conversions
            // that do not specify a conversion currency. This must be an ISO 4217
            // 3-character currency code such as "EUR" or "USD".
            // If this field is not set on this UploadConversion, AdWords will use
            // the account's currency.
            uploadConversion.defaultRevenueCurrencyCode = "EUR";

            // Optional: Set the default revenue value to use for conversions
            // that do not specify a conversion value. Note that this value
            // should NOT be in micros.
            uploadConversion.defaultRevenueValue = 2.50;

            // Optional: To upload fractional conversion credits, mark the upload conversion
            // as externally attributed. See
            // https://developers.google.com/adwords/api/docs/guides/conversion-tracking#importing_externally_attributed_conversions
            // to learn more about importing externally attributed conversions.

            // uploadConversion.isExternallyAttributed = true;

            conversionTrackers.Add(uploadConversion);

            try {
                // Create operations.
                List <ConversionTrackerOperation> operations = new List <ConversionTrackerOperation>();
                foreach (ConversionTracker conversionTracker in conversionTrackers)
                {
                    operations.Add(new ConversionTrackerOperation()
                    {
                        @operator = Operator.ADD,
                        operand   = conversionTracker
                    });
                }

                // Add conversion tracker.
                ConversionTrackerReturnValue retval = conversionTrackerService.mutate(
                    operations.ToArray());

                // Display the results.
                if (retval != null && retval.value != null)
                {
                    foreach (ConversionTracker conversionTracker in retval.value)
                    {
                        if (conversionTracker is AdWordsConversionTracker)
                        {
                            AdWordsConversionTracker newAdWordsConversionTracker =
                                (AdWordsConversionTracker)conversionTracker;
                            Console.WriteLine("Conversion with ID {0}, name '{1}', status '{2}', category " +
                                              "'{3}' and snippet '{4}' was added.",
                                              newAdWordsConversionTracker.id, newAdWordsConversionTracker.name,
                                              newAdWordsConversionTracker.status, newAdWordsConversionTracker.category,
                                              newAdWordsConversionTracker.snippet);
                        }
                        else
                        {
                            Console.WriteLine("Conversion with ID {0}, name '{1}', status '{2}' and category " +
                                              "'{3}' was added.", conversionTracker.id, conversionTracker.name,
                                              conversionTracker.status, conversionTracker.category);
                        }
                    }
                }
                else
                {
                    Console.WriteLine("No conversion trackers were added.");
                }
                conversionTrackerService.Close();
            } catch (Exception e) {
                throw new System.ApplicationException("Failed to add conversion trackers.", e);
            }
        }