@@ -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 ) ;
@@ -135,48 +133,52 @@ export default class Contentpass implements ContentpassInterface {
135133 } ;
136134
137135 public countImpression = async ( ) => {
138- if ( this . hasValidSubscriptionAndAccessToken ( ) ) {
139- try {
140- await this . countPaidImpression ( ) ;
141- } catch ( err : any ) {
142- reportError ( err , { msg : 'Failed to count paid impression' } ) ;
143- }
144- }
136+ await Promise . all ( [
137+ this . countPaidImpressionWhenUserHasValidSub ( ) ,
138+ this . countSampledImpression ( ) ,
139+ ] ) ;
140+ } ;
145141
146- try {
147- await this . countSampledImpression ( ) ;
148- } catch ( err : any ) {
149- reportError ( err , { msg : 'Failed to count sampled impression' } ) ;
142+ private countPaidImpressionWhenUserHasValidSub = async ( ) => {
143+ if ( ! this . hasValidSubscriptionAndAccessToken ( ) ) {
144+ return ;
150145 }
151- } ;
152146
153- private countPaidImpression = async ( ) => {
154147 logger . info ( 'Counting paid impression' ) ;
155148 const impressionId = uuid . v4 ( ) ;
156149
157- await sendPageViewEvent ( this . config . apiUrl , {
158- propertyId : this . config . propertyId ,
159- impressionId,
160- accessToken : this . oidcAuthState ! . accessToken ,
161- } ) ;
150+ try {
151+ await sendPageViewEvent ( this . config . apiUrl , {
152+ propertyId : this . config . propertyId ,
153+ impressionId,
154+ accessToken : this . oidcAuthState ! . accessToken ,
155+ } ) ;
156+ } catch ( err : any ) {
157+ reportError ( err , { msg : 'Failed to count paid impression' } ) ;
158+ }
162159 } ;
163160
164161 private countSampledImpression = async ( ) => {
165162 const generatedSample = Math . random ( ) ;
166163 const publicId = this . config . propertyId . slice ( 0 , 8 ) ;
164+ const instanceId = uuid . v4 ( ) ;
167165
168166 if ( generatedSample >= this . samplingRate ) {
169167 return ;
170168 }
171169
172170 logger . info ( 'Counting sampled impression' ) ;
173- await sendStats ( this . config . apiUrl , {
174- ea : 'load' ,
175- ec : 'tcf-sampled' ,
176- cpabid : this . instanceId ,
177- cppid : publicId ,
178- cpsr : this . samplingRate ,
179- } ) ;
171+ try {
172+ await sendStats ( this . config . apiUrl , {
173+ ea : 'load' ,
174+ ec : 'tcf-sampled' ,
175+ cpabid : instanceId ,
176+ cppid : publicId ,
177+ cpsr : this . samplingRate ,
178+ } ) ;
179+ } catch ( err : any ) {
180+ reportError ( err , { msg : 'Failed to count sampled impression' } ) ;
181+ }
180182 } ;
181183
182184 private initialiseAuthState = async ( ) => {
0 commit comments