private static string GetCustomEventsRule(CustomEventsRule rule)
        {
            if (rule == null)
            {
                return(null);
            }

            var rules = new List <string>();

            if (rule.Category != null)
            {
                rules.Add(string.Format("Category {0} {1}", rule.CategoryOperator, rule.Category));
            }

            if (rule.Action != null)
            {
                rules.Add(string.Format("Action {0} {1}", rule.ActionOperator, rule.Action));
            }

            if (rule.Label != null)
            {
                rules.Add(string.Format("Label {0} {1}", rule.LabelOperator, rule.Label));
            }

            if (rule.Value != null)
            {
                rules.Add(string.Format("Value {0} {1}", rule.ValueOperator, rule.Value));
            }

            if (rules.Count == 0)
            {
                throw new ArgumentException("Invalid Custom Events Rule");
            }

            return(string.Join(" and ", rules.Select(i => "(" + i + ")")));;
        }
        private static RemarketingRule ParseCustomEventsRule(string ruleStr)
        {
            if (string.IsNullOrEmpty(ruleStr))
            {
                return(null);
            }

            var rule = new CustomEventsRule {
                Type = "CustomEvents"
            };

            var ruleItemStrs = ruleStr.Split(new[] { @") and (" }, StringSplitOptions.None);

            foreach (var ruleItemStr in ruleItemStrs)
            {
                var temp = ruleItemStr.Replace("(", "").Replace(")", "");

                var match = OperandPattern.Match(temp);

                if (!match.Success)
                {
                    throw new ArgumentException(string.Format("Invalid Custom Events Rule Item: {0}", ruleStr));
                }

                var operand = match.Groups[1].Value.ToLower();

                var operatorStr = match.Groups[2].Value;

                if (operand.Equals("value"))
                {
                    var numberOperator = NumberOperatorPattern.Match(operatorStr);

                    if (!numberOperator.Success)
                    {
                        throw new ArgumentException(string.Format("Invalid Custom Events Rule Item Value Operator: {0}", operatorStr));
                    }

                    rule.ValueOperator = ParseNumberOperator(numberOperator.Groups[1].Value);

                    rule.Value = decimal.Parse(numberOperator.Groups[2].Value);
                }
                else
                {
                    var stringOperator = StringOperatorPattern.Match(operatorStr);

                    if (!stringOperator.Success)
                    {
                        throw new ArgumentException(string.Format("Invalid Custom Events Rule Item String Operator: {0}", operatorStr));
                    }

                    switch (operand)
                    {
                    case "category":
                        rule.CategoryOperator = ParseStringOperator(stringOperator.Groups[1].Value);
                        rule.Category         = stringOperator.Groups[2].Value;
                        break;

                    case "label":
                        rule.LabelOperator = ParseStringOperator(stringOperator.Groups[1].Value);
                        rule.Label         = stringOperator.Groups[2].Value;
                        break;

                    case "action":
                        rule.ActionOperator = ParseStringOperator(stringOperator.Groups[1].Value);
                        rule.Action         = stringOperator.Groups[2].Value;
                        break;

                    default:
                        throw new ArgumentException(string.Format("Invalid Custom Events Rule Item Operand: {0}", operatorStr));
                    }
                }
            }
            return(rule);
        }