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
@@ -45,6 +24,7 @@ static class Property
4524 IConnectionManager connectionManager ;
4625 readonly IPackageSettings userSettings ;
4726 IVSServices vsservices ;
27+ IUsageTracker visualStudioUsageTracker ;
4828 IDisposable timer ;
4929 bool firstTick = true ;
5030
@@ -77,27 +57,13 @@ public async Task IncrementCounter(Expression<Func<UsageModel.MeasuresModel, int
7757
7858 var updateTask = UpdateUsageMetrics ( propertyInfo ) ;
7959
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 ) )
60+ if ( visualStudioUsageTracker != null )
8961 {
90- counterName = counterName . Substring ( numberOfPrefix . Length ) ;
62+ // Not available on Visual Studio 2015
63+ await visualStudioUsageTracker . IncrementCounter ( counter ) ;
9164 }
9265
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 ) ;
66+ await updateTask ;
10167 }
10268
10369 bool IsEnterpriseUser =>
@@ -111,7 +77,7 @@ async Task UpdateUsageMetrics(PropertyInfo propertyInfo)
11177 var data = await service . ReadLocalData ( ) ;
11278 var usage = await GetCurrentReport ( data ) ;
11379
114- var value = ( int ) propertyInfo . GetValue ( usage . Measures ) ;
80+ var value = ( int ) propertyInfo . GetValue ( usage . Measures ) ;
11581 propertyInfo . SetValue ( usage . Measures , value + 1 ) ;
11682
11783 await service . WriteLocalData ( data ) ;
@@ -134,6 +100,15 @@ async Task Initialize()
134100 client = gitHubServiceProvider . TryGetService < IMetricsService > ( ) ;
135101 connectionManager = gitHubServiceProvider . GetService < IConnectionManager > ( ) ;
136102 vsservices = gitHubServiceProvider . GetService < IVSServices > ( ) ;
103+
104+ // Only create VisualStudioUsageTracker on Visual Studio 2017 and above
105+ var dte = gitHubServiceProvider . GetService < EnvDTE . DTE > ( ) ;
106+ if ( new Version ( dte . Version ) >= new Version ( 15 , 0 ) )
107+ {
108+ log . Verbose ( "Creating VisualStudioUsageTracker" ) ;
109+ visualStudioUsageTracker = new VisualStudioUsageTracker ( connectionManager ) ;
110+ }
111+
137112 initialized = true ;
138113 }
139114
0 commit comments