@@ -24,22 +24,40 @@ import '../lib/csp-detection'
2424import { shouldPolyfill } from '../lib/browser-polyfill'
2525import { RemoteMetrics } from '../core/stats/remote-metrics'
2626import { embeddedWriteKey } from '../lib/embedded-write-key'
27- import { onCSPError } from '../lib/csp-detection'
27+ import {
28+ loadAjsClassicFallback ,
29+ isAnalyticsCSPError ,
30+ } from '../lib/csp-detection'
31+
32+ let ajsIdentifiedCSP = false
33+
34+ const sendErrorMetrics = ( ( ) => {
35+ const metrics = new RemoteMetrics ( )
36+ return ( tags : string [ ] ) => {
37+ metrics . increment ( 'analytics_js.invoke.error' , [
38+ ...tags ,
39+ `wk:${ embeddedWriteKey ( ) } ` ,
40+ ] )
41+ }
42+ } ) ( )
2843
2944function onError ( err ?: unknown ) {
3045 console . error ( '[analytics.js]' , 'Failed to load Analytics.js' , err )
31-
32- new RemoteMetrics ( ) . increment ( 'analytics_js.invoke.error' , [
46+ sendErrorMetrics ( [
3347 'type:initialization' ,
3448 ...( err instanceof Error
3549 ? [ `message:${ err ?. message } ` , `name:${ err ?. name } ` ]
3650 : [ ] ) ,
37- `wk:${ embeddedWriteKey ( ) } ` ,
3851 ] )
3952}
4053
4154document . addEventListener ( 'securitypolicyviolation' , ( e ) => {
42- onCSPError ( e ) . catch ( console . error )
55+ if ( ajsIdentifiedCSP || ! isAnalyticsCSPError ( e ) ) {
56+ return
57+ }
58+ ajsIdentifiedCSP = true
59+ sendErrorMetrics ( [ 'type:csp' ] )
60+ loadAjsClassicFallback ( ) . catch ( console . error )
4361} )
4462
4563/**
0 commit comments