private static IDictionary<string, string> GetParametersDictionary(IGeneralParameters parameters)
        {
            var beaconList = new BeaconList<string, string>();

            foreach (var p in parameters.GetType().GetRuntimeProperties())
            {
                var attr = p.GetCustomAttribute(typeof(BeaconAttribute), true) as BeaconAttribute;

                if (attr == null) continue;

                object value;

                if ((p.PropertyType.GetTypeInfo().IsEnum || p.PropertyType.IsNullableEnum()) && attr.IsEnumByValueBased)
                    value = GetValueFromEnum(p, parameters) ?? p.GetMethod.Invoke(parameters, null);
                else if (p.PropertyType.GetTypeInfo().IsEnum || p.PropertyType.IsNullableEnum())
                    value = GetLowerCaseValueFromEnum(p, parameters) ?? p.GetMethod.Invoke(parameters, null);
                else
                    value = p.GetMethod.Invoke(parameters, null);

                if (value != null)
                    beaconList.Add(attr.Name, value.ToString());
            }

            beaconList.ShiftToLast("z");
            return beaconList.OrderBy(k => k.Item1, new BeaconComparer()).ToDictionary(key => key.Item1, value => value.Item2);
        }
 /// <summary>
 /// Set additional properties in parameters if they are empty: CacheBuster.
 /// The CacheBuster is set when UseHttpGet flag is set.
 /// <para>
 /// Override to change other properties.</para>
 /// </summary>
 /// <param name="parameters">GA request parameters.</param>
 protected virtual void AmendParameters(IGeneralParameters parameters)
 {
     if (UseHttpGet && string.IsNullOrEmpty(parameters.CacheBuster))
     {
         parameters.CacheBuster = AnalyticsSession.GenerateCacheBuster();
     }
 }
        private static object GetLowerCaseValueFromEnum(PropertyInfo propertyInfo, IGeneralParameters parameters)
        {
            var value = propertyInfo.GetMethod.Invoke(parameters, null);

            return(value == null
                ? null
                : value.ToString().ToLowerInvariant());
        }
        private static object GetLowerCaseValueFromEnum(PropertyInfo propertyInfo, IGeneralParameters parameters)
        {
            var value = propertyInfo.GetMethod.Invoke(parameters, null);

            return value == null 
                ? null 
                : value.ToString().ToLowerInvariant();
        }
Example #5
0
        public async Task <TrackingResult> TrackAsync(IGeneralParameters generalParameters)
        {
            SetRequired(generalParameters);

            var parameters = GetParametersDictionary(generalParameters);

            return(await RequestUrlAsync(UseSsl?BeaconUrlSsl : BeaconUrl, parameters, generalParameters.UserAgent ?? UserAgent));
        }
Example #6
0
 protected override void AmendParameters(IGeneralParameters parameters)
 {
     base.AmendParameters(parameters);
     if (string.IsNullOrEmpty(parameters.UserLanguage))
     {
         parameters.UserLanguage = UserLanguage;
     }
 }
        public async Task<TrackingResult> TrackAsync(IGeneralParameters generalParameters)
        {
            SetRequired(generalParameters);

            var parameters = GetParametersDictionary(generalParameters);

            return await RequestUrlAsync(UseSsl ? BeaconUrlSsl : BeaconUrl, parameters, generalParameters.UserAgent ?? UserAgent);
        }
        /// <summary>
        /// Send parameters to GA endpoint.
        /// </summary>
        /// <param name="generalParameters">GA request parameters.</param>
        /// <returns>Result of the request.</returns>
        public async Task <TrackingResult> TrackAsync(IGeneralParameters generalParameters)
        {
            AmendParameters(generalParameters);
            // Set required must come after amend.
            SetRequiredParameters(generalParameters);

            var parameters = GetParametersDictionary(generalParameters);

            return(await RequestUrlAsync(EndpointUrl, parameters, generalParameters.UserAgent ?? UserAgent));
        }
Example #9
0
        private void SetRequired(IGeneralParameters parameters)
        {
            parameters.TrackingId = TrackingAccount;
            parameters.ClientId   = AnalyticsSession.GenerateSessionId();

            if (string.IsNullOrWhiteSpace(parameters.UserLanguage))
            {
                // Note: another way could be CultureInfo.CurrentCulture
                parameters.UserLanguage = "en-US";
            }
        }
        private static object GetValueFromEnum(PropertyInfo propertyInfo, IGeneralParameters parameters)
        {
            var value = propertyInfo.GetMethod.Invoke(parameters, null);

            if (value == null) return null;

            var enumValue =
                Enum.Parse(propertyInfo.PropertyType.IsNullableEnum()
                        ? Nullable.GetUnderlyingType(propertyInfo.PropertyType)
                        : propertyInfo.PropertyType, value.ToString());

            return enumValue.GetHashCode().ToString(CultureInfo.InvariantCulture);
        }
        private void SetRequired(IGeneralParameters parameters)
        {
            parameters.TrackingId = TrackingAccount;

            if (string.IsNullOrWhiteSpace(parameters.ClientId))
            {
                parameters.ClientId = AnalyticsSession.GenerateSessionId();
            }

            if (string.IsNullOrWhiteSpace(parameters.UserLanguage))
            {
                // Note: another way could be CultureInfo.CurrentCulture
                parameters.UserLanguage = "en-US";
            }
        }        
        private static object GetValueFromEnum(PropertyInfo propertyInfo, IGeneralParameters parameters)
        {
            var value = propertyInfo.GetMethod.Invoke(parameters, null);

            if (value == null)
            {
                return(null);
            }

            var enumValue =
                Enum.Parse(propertyInfo.PropertyType.IsNullableEnum()
                        ? Nullable.GetUnderlyingType(propertyInfo.PropertyType)
                        : propertyInfo.PropertyType, value.ToString());

            return(enumValue.GetHashCode().ToString(CultureInfo.InvariantCulture));
        }
        private static IDictionary <string, string> GetParametersDictionary(IGeneralParameters parameters)
        {
            var beaconList = new BeaconList <string, string>();

            foreach (var p in parameters.GetType().GetRuntimeProperties())
            {
                var attr = p.GetCustomAttribute(typeof(BeaconAttribute), true) as BeaconAttribute;

                if (attr == null)
                {
                    continue;
                }

                object value;
                var    underlyingType = Nullable.GetUnderlyingType(p.PropertyType);

                if ((p.PropertyType.GetTypeInfo().IsEnum || p.PropertyType.IsNullableEnum()) && attr.IsEnumByValueBased)
                {
                    value = GetValueFromEnum(p, parameters) ?? p.GetMethod.Invoke(parameters, null);
                }
                else if (p.PropertyType.GetTypeInfo().IsEnum || p.PropertyType.IsNullableEnum())
                {
                    value = GetLowerCaseValueFromEnum(p, parameters) ?? p.GetMethod.Invoke(parameters, null);
                }
                else if (p.PropertyType == typeof(bool) || (underlyingType != null && underlyingType == typeof(bool)))
                {
                    value = p.GetMethod.Invoke(parameters, null);
                    if (value != null)
                    {
                        value = (bool)value ? "1" : "0";
                    }
                }
                else
                {
                    value = p.GetMethod.Invoke(parameters, null);
                }

                if (value == null)
                {
                    continue;
                }

                beaconList.Add(attr.Name, Convert.ToString(value, CultureInfo.InvariantCulture));
            }

            return(beaconList.ToDictionary(key => key.Item1, value => value.Item2));
        }
        /// <summary>
        /// Set base, required parameters if they are empty: TrackingId, ClientId.
        /// </summary>
        /// <param name="parameters">GA request parameters.</param>
        private void SetRequiredParameters(IGeneralParameters parameters)
        {
            if (string.IsNullOrEmpty(parameters.ProtocolVersion))
            {
                throw new ArgumentException("No ProtocolVersion", nameof(parameters));
            }

            if (string.IsNullOrEmpty(parameters.TrackingId))
            {
                parameters.TrackingId = TrackingAccount;
            }

            if (string.IsNullOrEmpty(parameters.ClientId))
            {
                parameters.ClientId = AnalyticsSession.GenerateSessionId();
            }
        }
        private static IDictionary <string, string> GetParametersDictionary(IGeneralParameters parameters)
        {
            var beaconList = new BeaconList <string, string>();

            foreach (var p in parameters.GetType().GetRuntimeProperties())
            {
                var attr = p.GetCustomAttribute(typeof(BeaconAttribute), true) as BeaconAttribute;

                if (attr == null)
                {
                    continue;
                }

                object value;

                if ((p.PropertyType.GetTypeInfo().IsEnum || p.PropertyType.IsNullableEnum()) && attr.IsEnumByValueBased)
                {
                    value = GetValueFromEnum(p, parameters) ?? p.GetMethod.Invoke(parameters, null);
                }
                else if (p.PropertyType.GetTypeInfo().IsEnum || p.PropertyType.IsNullableEnum())
                {
                    value = GetLowerCaseValueFromEnum(p, parameters) ?? p.GetMethod.Invoke(parameters, null);
                }
                else
                {
                    value = p.GetMethod.Invoke(parameters, null);
                }

                if (value != null)
                {
                    beaconList.Add(attr.Name, value.ToString());
                }
            }

            beaconList.ShiftToLast("z");

            return(beaconList
                   .OrderBy(k => k.Item1, new BeaconComparer())
                   .ToDictionary(key => key.Item1, value => value.Item2));
        }
 private void SetRequired(IGeneralParameters parameters)
 {
     parameters.TrackingId = TrackingAccount;
     parameters.ClientId = AnalyticsSession.GenerateSessionId();
 }        
Example #17
0
 private void SetRequired(IGeneralParameters parameters)
 {
     parameters.TrackingId = TrackingAccount;
     parameters.ClientId   = AnalyticsSession.GenerateSessionId();
 }