コード例 #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.v201409.AdwordsUserListService);

      // First rule item group - users who visited the checkout page and had
      // more than one item in their shopping cart.
      StringRuleItem checkoutStringRuleItem = new StringRuleItem();
      checkoutStringRuleItem.key = new StringKey();
      checkoutStringRuleItem.key.name = "ecomm_pagetype";
      checkoutStringRuleItem.op = StringRuleItemStringOperator.EQUALS;
      checkoutStringRuleItem.value = "checkout";

      RuleItem checkoutRuleItem = new RuleItem();
      checkoutRuleItem.Item = checkoutStringRuleItem;

      NumberRuleItem cartSizeNumberRuleItem = new NumberRuleItem();
      cartSizeNumberRuleItem.key = new NumberKey();
      cartSizeNumberRuleItem.key.name = "cartsize";
      cartSizeNumberRuleItem.op = NumberRuleItemNumberOperator.GREATER_THAN;
      cartSizeNumberRuleItem.value = 1;

      RuleItem cartSizeRuleItem = new RuleItem();
      cartSizeRuleItem.Item = cartSizeNumberRuleItem;

      // Combine the two rule items into a RuleItemGroup so AdWords will AND
      // their rules together.
      RuleItemGroup checkoutMultipleItemGroup = new RuleItemGroup();
      checkoutMultipleItemGroup.items = new RuleItem[] { checkoutRuleItem, cartSizeRuleItem };

      // Second rule item group - users who check out within the next 3 months.
      DateRuleItem startDateDateRuleItem = new DateRuleItem();
      startDateDateRuleItem.key = new DateKey();
      startDateDateRuleItem.key.name = "checkoutdate";
      startDateDateRuleItem.op = DateRuleItemDateOperator.AFTER;
      startDateDateRuleItem.value = DateTime.Now.ToString(DATE_FORMAT_STRING);
      RuleItem startDateRuleItem = new RuleItem();
      startDateRuleItem.Item = startDateDateRuleItem;

      DateRuleItem endDateDateRuleItem = new DateRuleItem();
      endDateDateRuleItem.key = new DateKey();
      endDateDateRuleItem.key.name = "checkoutdate";
      endDateDateRuleItem.op = DateRuleItemDateOperator.BEFORE;
      endDateDateRuleItem.value = DateTime.Now.AddMonths(3).ToString(DATE_FORMAT_STRING);
      RuleItem endDateRuleItem = new RuleItem();
      endDateRuleItem.Item = endDateDateRuleItem;

      // Combine the date rule items into a RuleItemGroup.
      RuleItemGroup checkedOutNextThreeMonthsItemGroup = new RuleItemGroup();
      checkedOutNextThreeMonthsItemGroup.items =
          new RuleItem[] { startDateRuleItem, endDateRuleItem };

      // Combine the rule item groups into a Rule so AdWords will OR the groups
      // together.
      Rule rule = new Rule();
      rule.groups = new RuleItemGroup[] {checkoutMultipleItemGroup,
        checkedOutNextThreeMonthsItemGroup};

      // Create the user list with no restrictions on site visit date.
      ExpressionRuleUserList expressionUserList = new ExpressionRuleUserList();
      expressionUserList.name = "Expression based user list created at " + DateTime.Now.ToString(
          "yyyyMMdd_HHmmss");
      expressionUserList.description = "Users who checked out in three month window OR visited " +
          "the checkout page with more than one item in their cart.";
      expressionUserList.rule = rule;

      // Create the user list restricted to users who visit your site within
      // the next six months.
      DateTime startDate = DateTime.Now;
      DateTime endDate = startDate.AddMonths(6);

      DateSpecificRuleUserList dateUserList = new DateSpecificRuleUserList();
      dateUserList.name = "Date rule user list created at " +
          DateTime.Now.ToString("yyyyMMdd_HHmmss");
      dateUserList.description = String.Format("Users who visited the site between {0} and " +
          "{1} and checked out in three month window OR visited the checkout page " +
          "with more than one item in their cart.", startDate.ToString(DATE_FORMAT_STRING),
          endDate.ToString(DATE_FORMAT_STRING));
      dateUserList.rule = rule;

      // Set the start and end dates of the user list.
      dateUserList.startDate = startDate.ToString(DATE_FORMAT_STRING);
      dateUserList.endDate = endDate.ToString(DATE_FORMAT_STRING);

      // Create operations to add the user lists.
      List<UserListOperation> operations = new List<UserListOperation>();
      foreach (UserList userList in new UserList[] { expressionUserList, dateUserList }) {
        UserListOperation operation = new UserListOperation();
        operation.operand = userList;
        operation.@operator = Operator.ADD;
        operations.Add(operation);
      }

      try {
        // Submit the operations.
        UserListReturnValue result = userListService.mutate(operations.ToArray());

        // Display the results.
        foreach (UserList userListResult in result.value) {
          Console.WriteLine("User list added with ID {0}, name '{1}', status '{2}', " +
              "list type '{3}', accountUserListStatus '{4}', description '{5}'.",
              userListResult.id,
              userListResult.name,
              userListResult.status,
              userListResult.listType,
              userListResult.accountUserListStatus,
              userListResult.description);
        }
      } catch (Exception ex) {
        throw new System.ApplicationException("Failed to add rule based user lists.", ex);
      }
    }
コード例 #2
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.v201409.AdwordsUserListService);

      // Get the ConversionTrackerService.
      ConversionTrackerService conversionTrackerService =
          (ConversionTrackerService)user.GetService(AdWordsService.v201409.
              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();
            selector.fields = new string[] {"Id"};

            Predicate conversionTypePredicate = new Predicate();
            conversionTypePredicate.field = "Id";
            conversionTypePredicate.@operator = PredicateOperator.IN;
            conversionTypePredicate.values = conversionIds.ToArray();
            selector.predicates = new Predicate[] {conversionTypePredicate};

            // 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 ex) {
        throw new System.ApplicationException("Failed to add user lists (a.k.a. audiences).", ex);
      }
    }