@@ -38,7 +38,6 @@ export default class Contentpass implements ContentpassInterface {
3838 private authStateStorage : OidcAuthStateStorage ;
3939 private readonly config : ContentpassConfig ;
4040 private readonly samplingRate : number ;
41- private readonly instanceId : string ;
4241
4342 private contentpassState : ContentpassState = {
4443 state : ContentpassStateType . INITIALISING ,
@@ -61,7 +60,6 @@ export default class Contentpass implements ContentpassInterface {
6160 throw new Error ( 'Sampling rate must be between 0 and 1' ) ;
6261 }
6362 this . samplingRate = config . samplingRate || DEFAULT_SAMPLING_RATE ;
64- this . instanceId = uuid . v4 ( ) ;
6563 this . authStateStorage = new OidcAuthStateStorage ( config . propertyId ) ;
6664 this . config = config ;
6765 setSentryExtraAttribute ( 'propertyId' , config . propertyId ) ;
@@ -134,48 +132,52 @@ export default class Contentpass implements ContentpassInterface {
134132 } ;
135133
136134 public countImpression = async ( ) => {
137- if ( this . hasValidSubscriptionAndAccessToken ( ) ) {
138- try {
139- await this . countPaidImpression ( ) ;
140- } catch ( err : any ) {
141- reportError ( err , { msg : 'Failed to count paid impression' } ) ;
142- }
143- }
135+ await Promise . all ( [
136+ this . countPaidImpressionWhenUserHasValidSub ( ) ,
137+ this . countSampledImpression ( ) ,
138+ ] ) ;
139+ } ;
144140
145- try {
146- await this . countSampledImpression ( ) ;
147- } catch ( err : any ) {
148- reportError ( err , { msg : 'Failed to count sampled impression' } ) ;
141+ private countPaidImpressionWhenUserHasValidSub = async ( ) => {
142+ if ( ! this . hasValidSubscriptionAndAccessToken ( ) ) {
143+ return ;
149144 }
150- } ;
151145
152- private countPaidImpression = async ( ) => {
153146 logger . info ( 'Counting paid impression' ) ;
154147 const impressionId = uuid . v4 ( ) ;
155148
156- await sendPageViewEvent ( this . config . apiUrl , {
157- propertyId : this . config . propertyId ,
158- impressionId,
159- accessToken : this . oidcAuthState ! . accessToken ,
160- } ) ;
149+ try {
150+ await sendPageViewEvent ( this . config . apiUrl , {
151+ propertyId : this . config . propertyId ,
152+ impressionId,
153+ accessToken : this . oidcAuthState ! . accessToken ,
154+ } ) ;
155+ } catch ( err : any ) {
156+ reportError ( err , { msg : 'Failed to count paid impression' } ) ;
157+ }
161158 } ;
162159
163160 private countSampledImpression = async ( ) => {
164161 const generatedSample = Math . random ( ) ;
165162 const publicId = this . config . propertyId . slice ( 0 , 8 ) ;
163+ const instanceId = uuid . v4 ( ) ;
166164
167165 if ( generatedSample >= this . samplingRate ) {
168166 return ;
169167 }
170168
171169 logger . info ( 'Counting sampled impression' ) ;
172- await sendStats ( this . config . apiUrl , {
173- ea : 'load' ,
174- ec : 'tcf-sampled' ,
175- cpabid : this . instanceId ,
176- cppid : publicId ,
177- cpsr : this . samplingRate ,
178- } ) ;
170+ try {
171+ await sendStats ( this . config . apiUrl , {
172+ ea : 'load' ,
173+ ec : 'tcf-sampled' ,
174+ cpabid : instanceId ,
175+ cppid : publicId ,
176+ cpsr : this . samplingRate ,
177+ } ) ;
178+ } catch ( err : any ) {
179+ reportError ( err , { msg : 'Failed to count sampled impression' } ) ;
180+ }
179181 } ;
180182
181183 private initialiseAuthState = async ( ) => {
0 commit comments