@@ -10,6 +10,9 @@ import { getSystemInfo, type SystemInfo } from './system-info.js';
1010export type AstroTelemetryOptions = { astroVersion : string ; viteVersion : string } ;
1111export type TelemetryEvent = { eventName : string ; payload : Record < string , any > } ;
1212
13+ // In the event of significant policy changes, update this!
14+ const VALID_TELEMETRY_NOTICE_DATE = '2023-08-25' ;
15+
1316type EventMeta = SystemInfo ;
1417interface EventContext extends ProjectInfo {
1518 anonymousId : string ;
@@ -20,6 +23,8 @@ export class AstroTelemetry {
2023 private _anonymousProjectInfo : ProjectInfo | undefined ;
2124 private config = new GlobalConfig ( { name : 'astro' } ) ;
2225 private debug = debug ( 'astro:telemetry' ) ;
26+ private isCI = isCI ;
27+ private env = process . env ;
2328
2429 private get astroVersion ( ) {
2530 return this . opts . astroVersion ;
@@ -28,10 +33,10 @@ export class AstroTelemetry {
2833 return this . opts . viteVersion ;
2934 }
3035 private get ASTRO_TELEMETRY_DISABLED ( ) {
31- return process . env . ASTRO_TELEMETRY_DISABLED ;
36+ return this . env . ASTRO_TELEMETRY_DISABLED ;
3237 }
3338 private get TELEMETRY_DISABLED ( ) {
34- return process . env . TELEMETRY_DISABLED ;
39+ return this . env . TELEMETRY_DISABLED ;
3540 }
3641
3742 constructor ( private opts : AstroTelemetryOptions ) {
@@ -47,7 +52,7 @@ export class AstroTelemetry {
4752 */
4853 private getConfigWithFallback < T > ( key : string , getValue : ( ) => T ) : T {
4954 const currentValue = this . config . get ( key ) ;
50- if ( currentValue ) {
55+ if ( currentValue !== undefined ) {
5156 return currentValue ;
5257 }
5358 const newValue = getValue ( ) ;
@@ -75,7 +80,7 @@ export class AstroTelemetry {
7580
7681 private get anonymousProjectInfo ( ) : ProjectInfo {
7782 // NOTE(fks): this value isn't global, so it can't use getConfigWithFallback().
78- this . _anonymousProjectInfo = this . _anonymousProjectInfo || getProjectInfo ( isCI ) ;
83+ this . _anonymousProjectInfo = this . _anonymousProjectInfo || getProjectInfo ( this . isCI ) ;
7984 return this . _anonymousProjectInfo ;
8085 }
8186
@@ -94,19 +99,29 @@ export class AstroTelemetry {
9499 return this . config . clear ( ) ;
95100 }
96101
97- async notify ( callback : ( ) => Promise < boolean > ) {
98- if ( this . isDisabled || isCI ) {
102+ isValidNotice ( ) {
103+ if ( ! this . notifyDate ) return false ;
104+ const current = Number ( this . notifyDate ) ;
105+ const valid = new Date ( VALID_TELEMETRY_NOTICE_DATE ) . valueOf ( ) ;
106+
107+ return current > valid ;
108+ }
109+
110+ async notify ( callback : ( ) => boolean | Promise < boolean > ) {
111+ if ( this . isDisabled || this . isCI ) {
112+ this . debug ( `[notify] telemetry has been disabled` ) ;
99113 return ;
100114 }
101115 // The end-user has already been notified about our telemetry integration!
102116 // Don't bother them about it again.
103- // In the event of significant changes, we should invalidate old dates.
104- if ( this . notifyDate ) {
117+ if ( this . isValidNotice ( ) ) {
118+ this . debug ( `[notify] last notified on ${ this . notifyDate } ` )
105119 return ;
106120 }
107121 const enabled = await callback ( ) ;
108- this . config . set ( KEY . TELEMETRY_NOTIFY_DATE , Date . now ( ) . toString ( ) ) ;
122+ this . config . set ( KEY . TELEMETRY_NOTIFY_DATE , new Date ( ) . valueOf ( ) . toString ( ) ) ;
109123 this . config . set ( KEY . TELEMETRY_ENABLED , enabled ) ;
124+ this . debug ( `[notify] telemetry has been ${ enabled ? 'enabled' : 'disabled' } ` )
110125 }
111126
112127 async record ( event : TelemetryEvent | TelemetryEvent [ ] = [ ] ) {
@@ -117,7 +132,7 @@ export class AstroTelemetry {
117132
118133 // Skip recording telemetry if the feature is disabled
119134 if ( this . isDisabled ) {
120- this . debug ( 'telemetry disabled' ) ;
135+ this . debug ( '[record] telemetry has been disabled' ) ;
121136 return Promise . resolve ( ) ;
122137 }
123138
0 commit comments