/// <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.v201502.
                                                          ConversionTrackerService);

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

            conversionTracker.name = "Earth to Mars Cruises Conversion #" +
                                     ExampleUtilities.GetRandomString();
            conversionTracker.category       = ConversionTrackerCategory.DEFAULT;
            conversionTracker.markupLanguage = AdWordsConversionTrackerMarkupLanguage.HTML;
            conversionTracker.textFormat     = AdWordsConversionTrackerTextFormat.HIDDEN;

            // Set optional fields.
            conversionTracker.status = ConversionTrackerStatus.ENABLED;
            conversionTracker.viewthroughLookbackWindow            = 15;
            conversionTracker.isProductAdsChargeable               = true;
            conversionTracker.productAdsChargeableConversionWindow = 15;
            conversionTracker.conversionPageLanguage               = "en";
            conversionTracker.backgroundColor              = "#0000FF";
            conversionTracker.defaultRevenueValue          = 23.41;
            conversionTracker.alwaysUseDefaultRevenueValue = true;

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

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

            try {
                // Add conversion tracker.
                ConversionTrackerReturnValue retval = conversionTrackerService.mutate(
                    new ConversionTrackerOperation[] { operation });

                // Display the results.
                if (retval != null && retval.value != null && retval.value.Length > 0)
                {
                    ConversionTracker newConversionTracker = retval.value[0];
                    Console.WriteLine("Conversion tracker with id '{0}', name '{1}', status '{2}', " +
                                      "category '{3}' was added.", newConversionTracker.id, newConversionTracker.name,
                                      newConversionTracker.status, newConversionTracker.category);
                }
                else
                {
                    Console.WriteLine("No conversion trackers were added.");
                }
            } catch (Exception ex) {
                throw new System.ApplicationException("Failed to add conversion tracker.", ex);
            }
        }
        /// <summary>
        /// Runs the code example.
        /// </summary>
        /// <param name="user">The AdWords user.</param>
        public void Run(AdWordsUser user)
        {
            using (AdwordsUserListService userListService =
                       (AdwordsUserListService)user.GetService(
                           AdWordsService.v201710.AdwordsUserListService))
                using (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.");
                        }
                    } catch (Exception e) {
                        throw new System.ApplicationException("Failed to add user lists (a.k.a. audiences).", e);
                    }
                }
        }
Example #3
0
        /// <summary>
        /// Runs the specified user.
        /// </summary>
        /// <param name="user">The user.</param>
        /// <param name="conversionName">The name of the upload conversion to be
        /// created.</param>
        /// <param name="gClid">The Google Click ID of the click for which offline
        /// conversions are uploaded.</param>
        /// <param name="conversionValue">The conversion value to be uploaded.
        /// </param>
        /// <param name="conversionTime">The conversion time, in yyyymmdd hhmmss
        /// format.</param>
        public void Run(AdWordsUser user, String conversionName, String gClid, String conversionTime,
                        double conversionValue)
        {
            // Get the ConversionTrackerService.
            ConversionTrackerService conversionTrackerService =
                (ConversionTrackerService)user.GetService(
                    AdWordsService.v201509.ConversionTrackerService);

            // Get the OfflineConversionFeedService.
            OfflineConversionFeedService offlineConversionFeedService =
                (OfflineConversionFeedService)user.GetService(
                    AdWordsService.v201509.OfflineConversionFeedService);

            const int VIEWTHROUGH_LOOKBACK_WINDOW = 30;
            const int CTC_LOOKBACK_WINDOW         = 90;

            try {
                // Create an upload conversion. Once created, this entry will be visible
                // under Tools and Analysis->Conversion and will have "Source = Import".
                UploadConversion uploadConversion = new UploadConversion();
                uploadConversion.category = ConversionTrackerCategory.PAGE_VIEW;
                uploadConversion.name     = conversionName;
                uploadConversion.viewthroughLookbackWindow = VIEWTHROUGH_LOOKBACK_WINDOW;
                uploadConversion.ctcLookbackWindow         = CTC_LOOKBACK_WINDOW;

                ConversionTrackerOperation uploadConversionOperation = new ConversionTrackerOperation();
                uploadConversionOperation.@operator = Operator.ADD;
                uploadConversionOperation.operand   = uploadConversion;

                ConversionTrackerReturnValue conversionTrackerRetval = conversionTrackerService.mutate(
                    new ConversionTrackerOperation[] { uploadConversionOperation });

                UploadConversion newUploadConversion = (UploadConversion)conversionTrackerRetval.value[0];

                Console.WriteLine("New upload conversion type with name = '{0}' and id = {1} was created.",
                                  newUploadConversion.name, newUploadConversion.id);

                // Associate offline conversions with the upload conversion we created.
                OfflineConversionFeed feed = new OfflineConversionFeed();
                feed.conversionName  = conversionName;
                feed.conversionTime  = conversionTime;
                feed.conversionValue = conversionValue;
                feed.googleClickId   = gClid;

                OfflineConversionFeedOperation offlineConversionOperation =
                    new OfflineConversionFeedOperation();
                offlineConversionOperation.@operator = Operator.ADD;
                offlineConversionOperation.operand   = feed;

                OfflineConversionFeedReturnValue offlineConversionRetval =
                    offlineConversionFeedService.mutate(
                        new OfflineConversionFeedOperation[] { offlineConversionOperation });

                OfflineConversionFeed newFeed = offlineConversionRetval.value[0];

                Console.WriteLine("Uploaded offline conversion value of {0} for Google Click ID = " +
                                  "'{1}' to '{2}'.", newFeed.conversionValue, newFeed.googleClickId,
                                  newFeed.conversionName);
            } catch (Exception e) {
                throw new System.ApplicationException("Failed upload offline conversions.", e);
            }
        }
        /// <summary>
        /// Runs the code example.
        /// </summary>
        /// <param name="user">The AdWords user.</param>
        public void Run(AdWordsUser user)
        {
            using (ConversionTrackerService conversionTrackerService =
                       (ConversionTrackerService)user.GetService(AdWordsService.v201802.
                                                                 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)
                        {
                            Console.WriteLine("Conversion with ID {0}, name '{1}', status '{2}' and " +
                                              "category '{3}' was added.", conversionTracker.id, conversionTracker.name,
                                              conversionTracker.status, conversionTracker.category);
                            if (conversionTracker is AdWordsConversionTracker)
                            {
                                AdWordsConversionTracker newAdWordsConversionTracker =
                                    (AdWordsConversionTracker)conversionTracker;
                                Console.WriteLine("Google global site tag:\n{0}\nGoogle event snippet:\n{1}",
                                                  newAdWordsConversionTracker.googleGlobalSiteTag,
                                                  newAdWordsConversionTracker.googleEventSnippet
                                                  );
                            }
                        }
                    }
                    else
                    {
                        Console.WriteLine("No conversion trackers were added.");
                    }
                } catch (Exception e) {
                    throw new System.ApplicationException("Failed to add conversion trackers.", e);
                }
            }
        }
Example #5
0
        /// <summary>
        /// Runs the code example.
        /// </summary>
        /// <param name="user">The AdWords user.</param>
        public void Run(AdWordsUser user)
        {
            using (AdwordsUserListService userListService =
                       (AdwordsUserListService)user.GetService(AdWordsService.v201802
                                                               .AdwordsUserListService))
                using (ConversionTrackerService conversionTrackerService =
                           (ConversionTrackerService)user.GetService(AdWordsService.v201802
                                                                     .ConversionTrackerService))
                {
                    BasicUserList userList = new BasicUserList
                    {
                        name               = "Mars cruise customers #" + ExampleUtilities.GetRandomString(),
                        description        = "A list of mars cruise customers in the last year.",
                        status             = UserListMembershipStatus.OPEN,
                        membershipLifeSpan = 365
                    };

                    UserListConversionType conversionType = new UserListConversionType
                    {
                        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
                    {
                        operand   = userList,
                        @operator = Operator.ADD
                    };

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

                        UserList newUserList = retval.value[0];

                        Console.WriteLine("User list with name '{0}' and id '{1}' was added.",
                                          newUserList.name, newUserList.id);

                        List <string> conversionIds = new List <string>();
                        Array.ForEach(userList.conversionTypes,
                                      delegate(UserListConversionType item)
                        {
                            conversionIds.Add(item.id.ToString());
                        });

                        // Create the selector.
                        Selector selector = new Selector()
                        {
                            fields = new string[]
                            {
                                ConversionTracker.Fields.Id,
                                ConversionTracker.Fields.GoogleGlobalSiteTag,
                                ConversionTracker.Fields.GoogleEventSnippet
                            },
                            predicates = new Predicate[]
                            {
                                Predicate.In(ConversionTracker.Fields.Id, conversionIds.ToArray())
                            }
                        };

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

                        if (page != null && page.entries != null)
                        {
                            foreach (ConversionTracker tracker in page.entries)
                            {
                                Console.WriteLine(
                                    "Google global site tag:\n{0}\nGoogle event snippet:\n{1}",
                                    tracker.googleGlobalSiteTag, tracker.googleGlobalSiteTag);
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        throw new System.ApplicationException(
                                  "Failed to add user lists (a.k.a. audiences).", e);
                    }
                }
        }
Example #6
0
        public static void CreateConversionTracker()
        {
            var user = new AdWordsUser();

            using (ConversionTrackerService conversionTrackerService =
                       (ConversionTrackerService)user.GetService(AdWordsService.v201710.ConversionTrackerService))
            {
                List <ConversionTracker> conversionTrackers = new List <ConversionTracker>();

                AdWordsConversionTracker adWordsConversionTracker = new AdWordsConversionTracker();
                adWordsConversionTracker.name = "Earth to Mars Cruises Conversion";

                adWordsConversionTracker.category = ConversionTrackerCategory.DEFAULT;
                // Set optional fields.
                adWordsConversionTracker.status = ConversionTrackerStatus.ENABLED;
                adWordsConversionTracker.viewthroughLookbackWindow    = 15;
                adWordsConversionTracker.defaultRevenueValue          = 23.41;
                adWordsConversionTracker.alwaysUseDefaultRevenueValue = true;

                conversionTrackers.Add(adWordsConversionTracker);


                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());

                    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.");
                    }
                }
                catch (Exception e)
                {
                    throw new System.ApplicationException("Failed to add conversion trackers.", e);
                }
            }
        }