/// <summary>
        /// Creates and returns a UserListRuleInfo object targeting a visit to a specified URL.
        /// </summary>
        /// <param name="url">The URL at which the rule will be targeted.</param>
        /// <returns>A populated UserListRuleInfo object.</returns>
        private UserListRuleInfo BuildVisitedSiteRuleInfo(string url)
        {
            // Creates a rule targeting any user that visited the specified URL.
            UserListRuleItemInfo userVisitedSiteRule = new UserListRuleItemInfo()
            {
                // Uses a built-in parameter to create a domain URL rule.
                Name           = "url__",
                StringRuleItem = new UserListStringRuleItemInfo()
                {
                    Operator = UserListStringRuleItemOperator.Equals,
                    Value    = url
                }
            };

            // Creates a UserListRuleInfo object containing the new rule.
            UserListRuleInfo userVisitedSiteRuleInfo = new UserListRuleInfo()
            {
                RuleItemGroups =
                {
                    new UserListRuleItemGroupInfo()
                    {
                        RuleItems ={ userVisitedSiteRule                 }
                    }
                }
            };

            return(userVisitedSiteRuleInfo);
        }
예제 #2
0
        /// <summary>
        /// Creates and returns a UserListRuleItemInfo object targeting a visit to a specified URL.
        /// </summary>
        /// <param name="url">The URL at which the rule will target users.</param>
        /// <returns>A populated UserListRuleItemInfo object.</returns>
        private UserListRuleItemInfo BuildVisitedSiteRuleInfo(string url)
        {
            // Creates a rule targeting any user that visited the specified URL.
            UserListRuleItemInfo userVisitedSiteRule = new UserListRuleItemInfo
            {
                // Uses a built-in parameter to create a domain URL rule.
                Name           = "url__",
                StringRuleItem = new UserListStringRuleItemInfo
                {
                    Operator = UserListStringRuleItemOperator.Contains,
                    Value    = url
                }
            };

            return(userVisitedSiteRule);
        }
예제 #3
0
        /// <summary>
        /// Runs the code example.
        /// </summary>
        /// <param name="client">The Google Ads client.</param>
        /// <param name="customerId">The Google Ads customer ID to which the new user list will be
        ///     added.</param>
        public void Run(GoogleAdsClient client, long customerId)
        {
            // Gets the UserListService.
            UserListServiceClient userListServiceClient =
                client.GetService(Services.V4.UserListService);

            // Creates the user targeting rules for each URL.
            UserListRuleItemInfo rule1 = BuildVisitedSiteRuleInfo("example.com/section1");
            UserListRuleItemInfo rule2 = BuildVisitedSiteRuleInfo("example.com/section2");

            // Combine the two rule items into a UserListRuleItemGroupInfo object so Google Ads will
            // AND their rules together. To instead OR the rules together, each rule should be
            // placed in its own rule item group.
            UserListRuleItemGroupInfo userListRuleItemGroupInfo = new UserListRuleItemGroupInfo();

            userListRuleItemGroupInfo.RuleItems.Add(rule1);
            userListRuleItemGroupInfo.RuleItems.Add(rule2);

            UserListRuleInfo userListRuleInfo = new UserListRuleInfo();

            userListRuleInfo.RuleItemGroups.Add(userListRuleItemGroupInfo);

            // Creates an ExpressionRuleUserListInfo object, or a boolean rule that defines this
            // user list. The default rule_type for a UserListRuleInfo object is OR of ANDs
            // (disjunctive normal form). That is, rule items will be ANDed together within rule
            // item groups and the groups themselves will be ORed together.
            ExpressionRuleUserListInfo expressionRuleUserListInfo = new ExpressionRuleUserListInfo
            {
                Rule = userListRuleInfo
            };

            // Defines a representation of a user list that is generated by a rule.
            RuleBasedUserListInfo ruleBasedUserListInfo = new RuleBasedUserListInfo
            {
                // Optional: To include past users in the user list, set the prepopulation_status to
                // REQUESTED.
                PrepopulationStatus    = UserListPrepopulationStatus.Requested,
                ExpressionRuleUserList = expressionRuleUserListInfo
            };

            // Creates a new user list.
            UserList userList = new UserList
            {
                Name = "All visitors to example.com/section1 AND example.com/section2 " +
                       $"#{ExampleUtilities.GetRandomString()}",
                Description        = "Visitors of both example.com/section1 AND example.com/section2",
                MembershipStatus   = UserListMembershipStatus.Open,
                MembershipLifeSpan = 365L,
                RuleBasedUserList  = ruleBasedUserListInfo
            };

            // Creates the operation.
            UserListOperation operation = new UserListOperation()
            {
                Create = userList
            };

            try
            {
                // Adds the user list.
                MutateUserListsResponse response = userListServiceClient.MutateUserLists
                                                       (customerId.ToString(), new[] { operation });

                // Displays the results.
                Console.WriteLine("Created new user list with resource name: " +
                                  $"'{response.Results.First().ResourceName}'.");
            }
            catch (GoogleAdsException e)
            {
                Console.WriteLine("Failure:");
                Console.WriteLine($"Message: {e.Message}");
                Console.WriteLine($"Failure: {e.Failure}");
                Console.WriteLine($"Request ID: {e.RequestId}");
                throw;
            }
        }
예제 #4
0
        /// <summary>
        /// Creates a user list targeting users that have visited a given url.
        /// </summary>
        /// <param name="client">The Google Ads API client.</param>
        /// <param name="customerId">The Google Ads customer ID for which the call is made.</param>
        /// <returns>The resource name of the newly created user list.</returns>
        // [START setup_remarketing]
        private string CreateUserList(GoogleAdsClient client, long customerId)
        {
            // Get the UserListService client.
            UserListServiceClient userListServiceClient =
                client.GetService(Services.V10.UserListService);

            // Create a rule targeting any user that visited a url containing 'example.com'.
            UserListRuleItemInfo rule = new UserListRuleItemInfo
            {
                // Use a built-in parameter to create a domain URL rule.
                Name           = "url__",
                StringRuleItem = new UserListStringRuleItemInfo
                {
                    Operator = UserListStringRuleItemOperator.Contains,
                    Value    = "example.com"
                }
            };

            // Specify that the user list targets visitors of a page based on the provided rule.
            ExpressionRuleUserListInfo expressionRuleUserListInfo = new ExpressionRuleUserListInfo
            {
                Rule = new UserListRuleInfo()
            };
            UserListRuleItemGroupInfo userListRuleItemGroupInfo = new UserListRuleItemGroupInfo();

            userListRuleItemGroupInfo.RuleItems.Add(rule);
            expressionRuleUserListInfo.Rule.RuleItemGroups.Add(userListRuleItemGroupInfo);

            // Define a representation of a user list that is generated by a rule.
            RuleBasedUserListInfo ruleBasedUserListInfo = new RuleBasedUserListInfo
            {
                // Optional: To include past users in the user list, set the prepopulation_status to
                // REQUESTED.
                PrepopulationStatus    = UserListPrepopulationStatus.Requested,
                ExpressionRuleUserList = expressionRuleUserListInfo
            };

            // Create the user list.
            UserList userList = new UserList
            {
                Name               = $"All visitors to example.com #{ExampleUtilities.GetRandomString()}",
                Description        = "Any visitor to any page of example.com",
                MembershipStatus   = UserListMembershipStatus.Open,
                MembershipLifeSpan = 365L,
                RuleBasedUserList  = ruleBasedUserListInfo
            };

            // Create the operation.
            UserListOperation userListOperation = new UserListOperation
            {
                Create = userList
            };

            // Add the user list, then print and return the new list's resource name.
            MutateUserListsResponse mutateUserListsResponse = userListServiceClient
                                                              .MutateUserLists(customerId.ToString(), new[] { userListOperation });
            string userListResourceName = mutateUserListsResponse.Results.First().ResourceName;

            Console.WriteLine($"Created user list with resource name '{userListResourceName}'.");

            return(userListResourceName);
        }
예제 #5
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>
        public void Run(GoogleAdsClient client, long customerId)
        {
            // Get the user list service client.
            UserListServiceClient userListServiceClient =
                client.GetService(Services.V6.UserListService);

            try
            {
                // Create a rule targeting any user that visited the checkout page.
                UserListRuleItemInfo checkoutRule = new UserListRuleItemInfo
                {
                    // The rule variable name must match a corresponding key name fired from a pixel.
                    // To learn more about setting up remarketing tags, visit
                    // https://support.google.com/google-ads/answer/2476688.
                    // To learn more about remarketing events and parameters, visit
                    // https://support.google.com/google-ads/answer/7305793.
                    Name           = "ecomm_pagetype",
                    StringRuleItem = new UserListStringRuleItemInfo
                    {
                        Operator = UserListStringRuleItemOperator.Equals,
                        Value    = "checkout"
                    }
                };

                // Create a rule targeting any user that had more than one item in their cart.
                UserListRuleItemInfo cartSizeRule = new UserListRuleItemInfo
                {
                    // The rule variable name must match a corresponding key name fired from a pixel.
                    Name           = "cart_size",
                    NumberRuleItem = new UserListNumberRuleItemInfo
                    {
                        Operator = UserListNumberRuleItemOperator.GreaterThan,
                        Value    = 1.0
                    }
                };

                // Create a rule group that includes the checkout and cart size rules. Combining the
                // two rule items into a UserListRuleItemGroupInfo object causes Google Ads to AND
                // their rules together. To instead OR the rules together, each rule should be
                // placed in its own rule item group.
                UserListRuleItemGroupInfo checkoutAndCartSizeRuleGroup =
                    new UserListRuleItemGroupInfo();
                checkoutAndCartSizeRuleGroup.RuleItems.Add(checkoutRule);
                checkoutAndCartSizeRuleGroup.RuleItems.Add(cartSizeRule);

                // Create the RuleItem for checkout start date.
                // The tags and keys used below must have been in place in the past for the date
                // range specified in the rules.
                UserListRuleItemInfo startDateRule = new UserListRuleItemInfo
                {
                    // The rule variable name must match a corresponding key name fired from a pixel.
                    Name         = "checkoutdate",
                    DateRuleItem = new UserListDateRuleItemInfo
                    {
                        // Available UserListDateRuleItemOperators can be found at
                        // https://developers.google.com/google-ads/api/reference/rpc/latest/UserListDateRuleItemOperatorEnum.UserListDateRuleItemOperator
                        Operator = UserListDateRuleItemOperator.After,
                        Value    = "20191031"
                    }
                };

                // Create the RuleItem for checkout end date.
                UserListRuleItemInfo endDateRule = new UserListRuleItemInfo
                {
                    // The rule variable name must match a corresponding key name fired from a pixel.
                    Name         = "checkoutdate",
                    DateRuleItem = new UserListDateRuleItemInfo
                    {
                        Operator = UserListDateRuleItemOperator.Before,
                        Value    = "20200101"
                    }
                };

                // Create a rule group targeting users who checked out between November and December
                // by using the start and end date rules. Combining the two rule items into a
                // UserListRuleItemGroupInfo object causes Google Ads to AND their rules together.
                // To instead OR the rules together, each rule should be placed in its own rule item
                // group.
                UserListRuleItemGroupInfo checkoutDateRuleGroup = new UserListRuleItemGroupInfo();
                checkoutDateRuleGroup.RuleItems.Add(startDateRule);
                checkoutDateRuleGroup.RuleItems.Add(endDateRule);

                // Create an ExpressionRuleUserListInfo object, or a boolean rule that defines this
                // user list. The default rule_type for a UserListRuleInfo object is OR of ANDs
                // (disjunctive normal form). That is, rule items will be ANDed together within rule
                // item groups and the groups themselves will be ORed together.
                ExpressionRuleUserListInfo expressionRuleUserListInfo =
                    new ExpressionRuleUserListInfo
                {
                    Rule = new UserListRuleInfo()
                };
                expressionRuleUserListInfo.Rule.RuleItemGroups.Add(checkoutAndCartSizeRuleGroup);
                expressionRuleUserListInfo.Rule.RuleItemGroups.Add(checkoutDateRuleGroup);

                // Defines a representation of a user list that is generated by a rule.
                RuleBasedUserListInfo ruleBasedUserListInfo = new RuleBasedUserListInfo
                {
                    // Optional: To include past users in the user list, set the
                    // prepopulation status to REQUESTED.
                    PrepopulationStatus    = UserListPrepopulationStatus.Requested,
                    ExpressionRuleUserList = expressionRuleUserListInfo
                };

                // Create a user list.
                UserList userList = new UserList
                {
                    Name        = $"My expression rule user list #{ExampleUtilities.GetRandomString()}",
                    Description =
                        "Users who checked out in November or December OR visited the checkout " +
                        "page with more than one item in their cart",
                    MembershipLifeSpan = 90L,
                    MembershipStatus   = UserListMembershipStatus.Open,
                    RuleBasedUserList  = ruleBasedUserListInfo
                };

                // Create the operation.
                UserListOperation userListOperation = new UserListOperation
                {
                    Create = userList
                };

                // Add the user list and print the results.
                MutateUserListsResponse response =
                    userListServiceClient.MutateUserLists(customerId.ToString(),
                                                          new[] { userListOperation });

                string userListResourceName = response.Results.First().ResourceName;
                Console.WriteLine(
                    $"Created user list with resource name '{userListResourceName}'.");
            }
            catch (GoogleAdsException e)
            {
                Console.WriteLine("Failure:");
                Console.WriteLine($"Message: {e.Message}");
                Console.WriteLine($"Failure: {e.Failure}");
                Console.WriteLine($"Request ID: {e.RequestId}");
                throw;
            }
        }