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));
        }
Пример #2
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));
        }