Example #1
0
        private async Task OnLocationChanged(string location)
        {
            var uri = new Uri(location);

            var options = new Segment.Model.Options();

            if (string.IsNullOrEmpty(AppState.UserId))
            {
                if (await AppState.LocalStorage.ContainKeyAsync("anonymous_id"))
                {
                    AppState.UserId = await AppState.LocalStorage.GetItemAsync <string>("anonymous_id");
                }
                else
                {
                    AppState.UserId = Guid.NewGuid().ToString();
                    await AppState.LocalStorage.SetItemAsync("anonymous_id", AppState.UserId);
                }
            }

            Segment.Analytics.Client.Page(AppState.UserId, Name,
                                          new Segment.Model.Properties()
            {
                { "Url", uri.AbsoluteUri },
                { "Path", uri.AbsolutePath },
                { "Title", Name },
                { "ClusterId", AppState.ClusterId }
            },
                                          options);

            await Task.CompletedTask;
        }
Example #2
0
        private void SendAnalyticsInfo(string username, string eventName, Dictionary <string, object> contextInfo)
        {
            string writeKey = Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["SegmentWriteKey"] ?? "W21J7U7JO9IXajS1w292q1atMOGklmPi");

            if (Segment.Analytics.Client == null)
            {
                Segment.Analytics.Initialize(writeKey, new Segment.Config().SetAsync(false));
            }

            //Segment.Analytics.Client.Track(ruleType, ruleName);

            //Segment.Analytics
            var req = System.Web.HttpContext.Current.Request;

            var options    = new Segment.Model.Options();
            var properties = new Segment.Model.Properties();

            Uri    referrer  = req.UrlReferrer;
            string userAgent = req.UserAgent;


            string ip = Durados.Web.Mvc.Logging.Logger.GetUserIP();

            const string utm_content  = "utm_content";
            const string utm_campaign = "utm_campaign";
            const string utm_medium   = "utm_medium";
            const string utm_source   = "utm_source";
            const string utm_term     = "utm_term";

            var campaign = new Segment.Model.Dict();


            if (referrer != null)
            {
                var query = referrer.ParseQueryString();
                if (query[utm_content] != null)
                {
                    campaign.Add("content", query[utm_content]);
                }
                if (query[utm_campaign] != null)
                {
                    campaign.Add("name", query[utm_campaign]);
                }
                if (query[utm_medium] != null)
                {
                    campaign.Add("medium", query[utm_medium]);
                }
                if (query[utm_source] != null)
                {
                    campaign.Add("source", query[utm_source]);
                }
                if (query[utm_term] != null)
                {
                    campaign.Add("keyword", query[utm_term]);
                }

                properties.Add("query", referrer.Query);
                properties.Add("path", referrer.PathAndQuery);
                properties.Add("host", referrer.Host);
                properties.Add("url", referrer.ToString());
            }
            foreach (string key in contextInfo.Keys)
            {
                properties.Add(key, contextInfo[key]);
            }
            /* ++ any custom props (eg. title) */

            var context = new Segment.Model.Context();

            context.Add("campaign", campaign);
            context.Add("userAgent", userAgent);
            context.Add("ip", Durados.Web.Mvc.Logging.Logger.UserIPAddress);


            options.SetContext(context);

            Segment.Analytics.Client.Track(username, eventName, properties, options);
        }