public static MessageBuildResult Build(
            string token,
            IEnumerable <ObjectProperty> superProperties,
            object distinctId,
            object alias)
        {
            MessageCandidate messageCandidate = TrackMessageBuilderBase.CreateValidMessageCandidate(
                token,
                superProperties,
                null,
                distinctId,
                null,
                out string messageCandidateErrorMessage);

            if (messageCandidate == null)
            {
                return(MessageBuildResult.CreateFail(messageCandidateErrorMessage));
            }

            var message = new Dictionary <string, object>(2);

            message["event"] = "$create_alias";

            var properties = new Dictionary <string, object>(3);

            message["properties"] = properties;

            // token
            properties["token"] = messageCandidate.GetSpecialProperty(TrackSpecialProperty.Token).Value;

            // distinct_id
            ObjectProperty rawDistinctId = messageCandidate.GetSpecialProperty(TrackSpecialProperty.DistinctId);

            if (rawDistinctId == null)
            {
                return(MessageBuildResult.CreateFail($"'{TrackSpecialProperty.DistinctId}' is not set."));
            }

            ValueParseResult distinctIdParseResult = DistinctIdParser.Parse(rawDistinctId.Value);

            if (!distinctIdParseResult.Success)
            {
                return(MessageBuildResult.CreateFail(
                           $"Error parsing '{TrackSpecialProperty.DistinctId}'.", distinctIdParseResult.ErrorDetails));
            }

            properties["distinct_id"] = distinctIdParseResult.Value;

            // alias
            ValueParseResult aliasParseResult = DistinctIdParser.Parse(alias);

            if (!aliasParseResult.Success)
            {
                return(MessageBuildResult.CreateFail("Error parsing 'alias'. " + aliasParseResult.ErrorDetails));
            }
            properties["alias"] = aliasParseResult.Value;


            return(MessageBuildResult.CreateSuccess(message));
        }
        private static MessageBuildResult Build(
            string operation,
            string token,
            IEnumerable <ObjectProperty> superProperties,
            object rawProperties,
            object distinctId,
            MixpanelConfig config)
        {
            MessageCandidate messageCandidate = PeopleMessageBuilderBase.CreateValidMessageCandidate(
                token,
                superProperties,
                rawProperties,
                distinctId,
                config,
                out string messageCandidateErrorMessage);

            if (messageCandidate == null)
            {
                return(MessageBuildResult.CreateFail(messageCandidateErrorMessage));
            }

            var message = new Dictionary <string, object>();
            var set     = new Dictionary <string, object>();

            message[operation] = set;

            // Special properties
            PeopleMessageBuilderBase.RunForValidSpecialProperties(
                messageCandidate,
                (specialPropertyName, isMessageSpecialProperty, value) =>
            {
                if (isMessageSpecialProperty)
                {
                    message[specialPropertyName] = value;
                }
                else
                {
                    set[specialPropertyName] = value;
                }
            });

            // User properties
            PeopleMessageBuilderBase.RunForValidUserProperties(
                messageCandidate,
                rawValue => GenericPropertyParser.Parse(rawValue, allowCollections: true),
                (formattedPropertyName, value) =>
            {
                set[formattedPropertyName] = value;
            });

            return(MessageBuildResult.CreateSuccess(message));
        }
Example #3
0
        public static MessageBuildResult CreateMessage(
            string token,
            IEnumerable <ObjectProperty> superProperties,
            object rawProperties,
            object distinctId,
            MixpanelConfig config,
            string actionName,
            Func <object, ValueParseResult> userPropertyParseFn)
        {
            MessageCandidate messageCandidate = CreateValidMessageCandidate(
                token,
                superProperties,
                rawProperties,
                distinctId,
                config,
                out string messageCandidateErrorMessage);

            if (messageCandidate == null)
            {
                return(MessageBuildResult.CreateFail(messageCandidateErrorMessage));
            }

            var message = new Dictionary <string, object>();
            var action  = new Dictionary <string, object>();

            message[actionName] = action;

            // Special properties
            RunForValidSpecialProperties(
                messageCandidate,
                (specialPropertyName, isMessageSpecialProperty, value) =>
            {
                // Ignore non-message specific special properties as they are not valid in profile update messages
                if (isMessageSpecialProperty)
                {
                    message[specialPropertyName] = value;
                }
            });

            // User properties
            RunForValidUserProperties(
                messageCandidate,
                userPropertyParseFn,
                (formattedPropertyName, value) =>
            {
                action[formattedPropertyName] = value;
            });

            return(MessageBuildResult.CreateSuccess(message));
        }
        // Message example:
        // {
        //     "$token": "36ada5b10da39a1347559321baf13063",
        //     "$distinct_id": "13793",
        //     "$unset": [ "Days Overdue" ]
        // }

        public static MessageBuildResult Build(
            string token,
            IEnumerable <ObjectProperty> superProperties,
            IEnumerable <string> propertyNames,
            object distinctId,
            MixpanelConfig config)
        {
            MessageCandidate messageCandidate = PeopleMessageBuilderBase.CreateValidMessageCandidate(
                token,
                superProperties,
                null,
                distinctId,
                config,
                out string messageCandidateErrorMessage);

            if (messageCandidate == null)
            {
                return(MessageBuildResult.CreateFail(messageCandidateErrorMessage));
            }

            var message = new Dictionary <string, object>();

            // Special properties
            PeopleMessageBuilderBase.RunForValidSpecialProperties(
                messageCandidate,
                (specialPropertyName, isMessageSpecialProperty, value) =>
            {
                // Ignore non-message specific special properties as they are not valid in profile update messages
                if (isMessageSpecialProperty)
                {
                    message[specialPropertyName] = value;
                }
            });

            message["$unset"] = propertyNames ?? new string[0];

            return(MessageBuildResult.CreateSuccess(message));
        }
Example #5
0
        public static MessageBuildResult Build(
            string token,
            string @event,
            IEnumerable <ObjectProperty> superProperties,
            object rawProperties,
            object distinctId,
            MixpanelConfig config)
        {
            if (string.IsNullOrWhiteSpace(@event))
            {
                return(MessageBuildResult.CreateFail($"'{nameof(@event)}' is not set."));
            }

            MessageCandidate messageCandidate = TrackMessageBuilderBase.CreateValidMessageCandidate(
                token,
                superProperties,
                rawProperties,
                distinctId,
                config,
                out string messageCandidateErrorMessage);

            if (messageCandidate == null)
            {
                return(MessageBuildResult.CreateFail(messageCandidateErrorMessage));
            }

            var message = new Dictionary <string, object>(2);

            message["event"] = @event;

            var properties = new Dictionary <string, object>();

            message["properties"] = properties;

            // Special properties
            foreach (KeyValuePair <string, ObjectProperty> pair in messageCandidate.SpecialProperties)
            {
                string         specialPropertyName = pair.Key;
                ObjectProperty objectProperty      = pair.Value;

                ValueParseResult result =
                    TrackSpecialPropertyParser.Parse(specialPropertyName, objectProperty.Value);
                if (!result.Success)
                {
                    // The only required special properties are 'event' and 'token' which are controlled separately
                    continue;
                }

                properties[specialPropertyName] = result.Value;
            }

            // User properties
            foreach (KeyValuePair <string, ObjectProperty> pair in messageCandidate.UserProperties)
            {
                string         formattedPropertyName = pair.Key;
                ObjectProperty objectProperty        = pair.Value;

                ValueParseResult result = GenericPropertyParser.Parse(objectProperty.Value, allowCollections: true);
                if (!result.Success)
                {
                    continue;
                }

                properties[formattedPropertyName] = result.Value;
            }

            return(MessageBuildResult.CreateSuccess(message));
        }