77using GitHub . Logging ;
88using GitHub . Models ;
99using GitHub . Settings ;
10- using Microsoft . VisualStudio . Telemetry ;
11- using Microsoft . VisualStudio . Text . Editor ;
1210using Microsoft . VisualStudio . Threading ;
1311using Serilog ;
1412using Task = System . Threading . Tasks . Task ;
@@ -17,25 +15,6 @@ namespace GitHub.Services
1715{
1816 public sealed class UsageTracker : IUsageTracker , IDisposable
1917 {
20- private const int TelemetryVersion = 1 ; // Please update the version every time you want to indicate a change in telemetry logic when the extension itself is updated
21-
22- private const string EventNamePrefix = "vs/github/usagetracker/" ;
23- private const string PropertyPrefix = "vs.github." ;
24-
25- static class Event
26- {
27- public const string UsageTracker = EventNamePrefix + "increment-counter" ;
28- }
29-
30- static class Property
31- {
32- public const string TelemetryVersion = PropertyPrefix + nameof ( TelemetryVersion ) ;
33- public const string CounterName = PropertyPrefix + nameof ( CounterName ) ;
34- public const string ExtensionVersion = PropertyPrefix + nameof ( ExtensionVersion ) ;
35- public const string IsGitHubUser = PropertyPrefix + nameof ( IsGitHubUser ) ;
36- public const string IsEnterpriseUser = PropertyPrefix + nameof ( IsEnterpriseUser ) ;
37- }
38-
3918 static readonly ILogger log = LogManager . ForContext < UsageTracker > ( ) ;
4019 readonly IGitHubServiceProvider gitHubServiceProvider ;
4120
@@ -44,20 +23,25 @@ static class Property
4423 readonly IUsageService service ;
4524 IConnectionManager connectionManager ;
4625 readonly IPackageSettings userSettings ;
26+ readonly bool vsTelemetry ;
4727 IVSServices vsservices ;
28+ IUsageTracker visualStudioUsageTracker ;
4829 IDisposable timer ;
4930 bool firstTick = true ;
5031
5132 public UsageTracker (
5233 IGitHubServiceProvider gitHubServiceProvider ,
5334 IUsageService service ,
5435 IPackageSettings settings ,
55- JoinableTaskContext joinableTaskContext )
36+ JoinableTaskContext joinableTaskContext ,
37+ bool vsTelemetry )
5638 {
5739 this . gitHubServiceProvider = gitHubServiceProvider ;
5840 this . service = service ;
5941 this . userSettings = settings ;
6042 JoinableTaskContext = joinableTaskContext ;
43+ this . vsTelemetry = vsTelemetry ;
44+
6145 timer = StartTimer ( ) ;
6246 }
6347
@@ -77,27 +61,13 @@ public async Task IncrementCounter(Expression<Func<UsageModel.MeasuresModel, int
7761
7862 var updateTask = UpdateUsageMetrics ( propertyInfo ) ;
7963
80- LogTelemetryEvent ( counterName ) ;
81-
82- await updateTask ;
83- }
84-
85- void LogTelemetryEvent ( string counterName )
86- {
87- const string numberOfPrefix = "numberof" ;
88- if ( counterName . StartsWith ( numberOfPrefix , StringComparison . OrdinalIgnoreCase ) )
64+ if ( visualStudioUsageTracker != null )
8965 {
90- counterName = counterName . Substring ( numberOfPrefix . Length ) ;
66+ // Not available on Visual Studio 2015
67+ await visualStudioUsageTracker . IncrementCounter ( counter ) ;
9168 }
9269
93- var operation = new TelemetryEvent ( Event . UsageTracker ) ;
94- operation . Properties [ Property . TelemetryVersion ] = TelemetryVersion ;
95- operation . Properties [ Property . CounterName ] = counterName ;
96- operation . Properties [ Property . ExtensionVersion ] = AssemblyVersionInformation . Version ;
97- operation . Properties [ Property . IsGitHubUser ] = IsGitHubUser ;
98- operation . Properties [ Property . IsEnterpriseUser ] = IsEnterpriseUser ;
99-
100- TelemetryService . DefaultSession . PostEvent ( operation ) ;
70+ await updateTask ;
10171 }
10272
10373 bool IsEnterpriseUser =>
@@ -111,7 +81,7 @@ async Task UpdateUsageMetrics(PropertyInfo propertyInfo)
11181 var data = await service . ReadLocalData ( ) ;
11282 var usage = await GetCurrentReport ( data ) ;
11383
114- var value = ( int ) propertyInfo . GetValue ( usage . Measures ) ;
84+ var value = ( int ) propertyInfo . GetValue ( usage . Measures ) ;
11585 propertyInfo . SetValue ( usage . Measures , value + 1 ) ;
11686
11787 await service . WriteLocalData ( data ) ;
@@ -134,6 +104,13 @@ async Task Initialize()
134104 client = gitHubServiceProvider . TryGetService < IMetricsService > ( ) ;
135105 connectionManager = gitHubServiceProvider . GetService < IConnectionManager > ( ) ;
136106 vsservices = gitHubServiceProvider . GetService < IVSServices > ( ) ;
107+
108+ if ( vsTelemetry )
109+ {
110+ log . Verbose ( "Creating VisualStudioUsageTracker" ) ;
111+ visualStudioUsageTracker = new VisualStudioUsageTracker ( connectionManager ) ;
112+ }
113+
137114 initialized = true ;
138115 }
139116
0 commit comments