Skip to content

Commit 2856896

Browse files
sakshamg1304rohitesh-wingify
authored andcommitted
feat: feature Usage
1 parent 206505b commit 2856896

File tree

10 files changed

+195
-11
lines changed

10 files changed

+195
-11
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [1.8.1] - 2025-05-21
9+
10+
### Added
11+
12+
- Added a feature to track and collect usage statistics related to various SDK features and configurations which can be useful for analytics, and gathering insights into how different features are being utilized by end users.
13+
814
## [1.8.0] - 2025-05-21
915

1016
### Added
@@ -123,4 +129,4 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
123129

124130
// Send attribute data
125131
vwoInstance.SetAttribute("attribute-key", "attribute-value" , context);
126-
```
132+
```

VWOFmeSdk.NetStandard2.0/VWOFmeSdk.NetStandard2.0.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<AssemblyName>VWOFmeSdk</AssemblyName>
77
<TargetFramework>netstandard2.0</TargetFramework>
88
<PackageId>VWO.FME.Sdk</PackageId>
9-
<Version>1.8.0</Version>
9+
<Version>1.8.1</Version>
1010
<Authors>VWO devs</Authors>
1111
<Company>Wingify</Company>
1212
<Product>VWO</Product>

VWOFmeSdk/Models/Request/Props.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ public class Props
4949
[JsonProperty("data")]
5050
public object Data { get; set; } // Added explicitly for "data"
5151

52+
[JsonProperty("vwoMeta")]
53+
public Dictionary<string, object> VwoMeta { get; set; }
54+
5255
[JsonExtensionData]
5356
public Dictionary<string, object> AdditionalProperties { get; set; } = new Dictionary<string, object>();
5457
}

VWOFmeSdk/Models/User/VWOInitOptions.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ public class VWOInitOptions
3737
private VWOBuilder vwoBuilder;
3838
private Dictionary<string, object> gatewayService = new Dictionary<string, object>();
3939
private BatchEventData batchEventData;
40+
private bool isUsageStatsDisabled;
41+
private Dictionary<string, object> vwoMetaData = new Dictionary<string, object>();
4042

4143
private string settings;
4244

@@ -111,5 +113,17 @@ public BatchEventData BatchEventData
111113
get { return batchEventData; }
112114
set { batchEventData = value; }
113115
}
116+
117+
public bool IsUsageStatsDisabled
118+
{
119+
get { return isUsageStatsDisabled; }
120+
set { isUsageStatsDisabled = value; }
121+
}
122+
123+
public Dictionary<string, object> VwoMetaData
124+
{
125+
get { return vwoMetaData; }
126+
set { vwoMetaData = value; }
127+
}
114128
}
115-
}
129+
}

VWOFmeSdk/Packages/Logger/Enums/LogLevelNumberEnum.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ namespace VWOFmeSdk.Packages.Logger.Enums
2020
{
2121
public enum LogLevelNumberEnum
2222
{
23-
Trace = 0,
24-
Debug = 1,
25-
Info = 2,
26-
Warn = 3,
27-
Error = 4
23+
TRACE = 0,
24+
DEBUG = 1,
25+
INFO = 2,
26+
WARN = 3,
27+
ERROR = 4
2828
}
2929
}

VWOFmeSdk/Packages/NetworkLayer/Manager/NetworkManager.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
using VWOFmeSdk.Services;
2626
using VWOFmeSdk.Packages.Logger.Enums;
2727
using VWOFmeSdk.Interfaces.Batching;
28+
using VWOFmeSdk.Utils;
2829

2930
namespace VWOFmeSdk.Packages.NetworkLayer.Manager
3031
{
@@ -153,7 +154,17 @@ public ResponseModel Post(RequestModel request, IFlushInterface flushCallback =
153154
/// <param name="request">The RequestModel containing the URL, headers, and body of the POST request.</param>
154155
public void PostAsync(RequestModel request)
155156
{
156-
executorService.StartNew(() => Post(request));
157+
executorService.StartNew(() =>
158+
{
159+
var response = Post(request);
160+
if (response != null && response.GetStatusCode() >= 200 && response.GetStatusCode() <= 299)
161+
{
162+
if (UsageStatsUtil.GetInstance().GetUsageStats().Count > 0)
163+
{
164+
UsageStatsUtil.GetInstance().ClearUsageStats();
165+
}
166+
}
167+
});
157168
}
158169
}
159170
}

VWOFmeSdk/Utils/NetworkUtil.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,11 @@ public static Dictionary<string, object> GetTrackUserPayloadData(Settings settin
186186
properties.D.Event.Props.Id = campaignId;
187187
properties.D.Event.Props.Variation = variationId.ToString(CultureInfo.InvariantCulture);
188188
properties.D.Event.Props.IsFirst = 1;
189+
190+
if (UsageStatsUtil.GetInstance().GetUsageStats().Count > 0)
191+
{
192+
properties.D.Event.Props.VwoMeta = UsageStatsUtil.GetInstance().GetUsageStats();
193+
}
189194

190195
LoggerService.Log(LogLevelEnum.DEBUG, "IMPRESSION_FOR_TRACK_USER", new Dictionary<string, string>
191196
{

VWOFmeSdk/Utils/UsageStatsUtil.cs

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using VWOFmeSdk.Models.User;
4+
using VWOFmeSdk.Packages.Logger.Enums;
5+
6+
namespace VWOFmeSdk.Utils
7+
{
8+
/// <summary>
9+
/// Manages usage statistics for the SDK.
10+
/// Tracks various features and configurations being used by the client.
11+
/// Implements Singleton pattern to ensure a single instance.
12+
/// </summary>
13+
public class UsageStatsUtil
14+
{
15+
/// <summary>
16+
/// Singleton instance
17+
/// </summary>
18+
private static UsageStatsUtil _instance;
19+
20+
/// <summary>
21+
/// Internal storage for usage statistics data
22+
/// </summary>
23+
private Dictionary<string, object> _usageStatsData;
24+
25+
/// <summary>
26+
/// Private constructor to prevent direct instantiation
27+
/// </summary>
28+
private UsageStatsUtil()
29+
{
30+
_usageStatsData = new Dictionary<string, object>();
31+
}
32+
33+
/// <summary>
34+
/// Provides access to the singleton instance of UsageStatsUtil
35+
/// </summary>
36+
/// <returns>The single instance of UsageStatsUtil</returns>
37+
public static UsageStatsUtil GetInstance()
38+
{
39+
if (_instance == null)
40+
{
41+
_instance = new UsageStatsUtil();
42+
}
43+
return _instance;
44+
}
45+
46+
/// <summary>
47+
/// Sets usage statistics based on provided options
48+
/// </summary>
49+
/// <param name="options">Configuration options for the SDK</param>
50+
public void SetUsageStats(VWOInitOptions options)
51+
{
52+
var data = new Dictionary<string, object>();
53+
54+
// Map configuration options to usage stats flags
55+
if (options.Integrations != null) data["ig"] = 1;
56+
57+
// Check if the logger has transports in it
58+
if (options.Logger != null && options.Logger is Dictionary<string, object> loggerDict)
59+
{
60+
if (loggerDict.ContainsKey("transport") || loggerDict.ContainsKey("transports"))
61+
{
62+
data["cl"] = 1;
63+
}
64+
}
65+
66+
// Check the logger level
67+
if (options.Logger != null && options.Logger is Dictionary<string, object> loggerMap)
68+
{
69+
if (loggerMap.ContainsKey("level"))
70+
{
71+
string level = loggerMap["level"].ToString();
72+
try
73+
{
74+
if (Enum.TryParse<LogLevelNumberEnum>(level.ToUpper(), out var logLevelEnum))
75+
{
76+
data["ll"] = (int)logLevelEnum;
77+
}
78+
else
79+
{
80+
data["ll"] = -1;
81+
}
82+
}
83+
catch
84+
{
85+
data["ll"] = -1;
86+
}
87+
}
88+
}
89+
90+
if (options.Storage != null) data["ss"] = 1;
91+
if (options.GatewayService != null) data["gs"] = 1;
92+
if (options.PollInterval != null) data["pi"] = 1;
93+
94+
// Handle _vwo_meta
95+
if (options.VwoMetaData != null && options.VwoMetaData is Dictionary<string, object> vwoMetaMap)
96+
{
97+
if (vwoMetaMap.ContainsKey("ea"))
98+
{
99+
data["_ea"] = 1;
100+
}
101+
}
102+
103+
// Get .NET version
104+
data["lv"] = Environment.Version.ToString();
105+
106+
_usageStatsData = data;
107+
}
108+
109+
/// <summary>
110+
/// Retrieves the current usage statistics
111+
/// </summary>
112+
/// <returns>Dictionary containing flags for various SDK features in use</returns>
113+
public Dictionary<string, object> GetUsageStats()
114+
{
115+
return new Dictionary<string, object>(_usageStatsData);
116+
}
117+
118+
/// <summary>
119+
/// Clears the usage statistics data
120+
/// </summary>
121+
public void ClearUsageStats()
122+
{
123+
_usageStatsData.Clear();
124+
}
125+
}
126+
}

VWOFmeSdk/VWO.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ private static VWO SetInstance(VWOInitOptions options)
6161
.SetStorage()
6262
.SetNetworkManager()
6363
.SetSegmentation()
64-
.InitPolling();
64+
.InitPolling()
65+
.InitUsageStats();
6566

6667
string settings;
6768
if (!string.IsNullOrEmpty(options.Settings))

VWOFmeSdk/VWOBuilder.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
using VWOFmeSdk.Services;
3232
using VWOFmeSdk.Constants;
3333
using ConstantsNamespace = VWOFmeSdk.Constants;
34+
using VWOFmeSdk.Utils;
3435

3536
namespace VWOFmeSdk
3637
{
@@ -355,6 +356,23 @@ public VWOBuilder InitBatching()
355356
return this;
356357
}
357358

359+
/// <summary>
360+
/// Initializes the usage stats for the VWO instance.
361+
/// </summary>
362+
/// <returns>The instance of this builder.</returns>
363+
public VWOBuilder InitUsageStats()
364+
{
365+
// if usageStatsDisabled is not null and is true, then return
366+
if (this.options.IsUsageStatsDisabled)
367+
{
368+
return this;
369+
}
370+
371+
UsageStatsUtil.GetInstance().SetUsageStats(this.options);
372+
373+
return this;
374+
}
375+
358376
/// <summary>
359377
/// Checks and polls for settings updates at the provided interval.
360378
/// </summary>
@@ -413,4 +431,4 @@ private static Dictionary<string, object> ConvertLoggerOptions(Dictionary<string
413431
return convertedLoggerOptions;
414432
}
415433
}
416-
}
434+
}

0 commit comments

Comments
 (0)