1+ ( function ( w , d ) {
2+ var c = w . Countly || ( w . Countly = { } ) ;
3+ var bootstrapMeta = {
4+ version : "1.0.0" ,
5+ name : "countly-unified-loader"
6+ } ;
7+ var pendingCalls = [ ] ;
8+ var isLoading = false ;
9+ var isLoaded = false ;
10+ var initConfig = null ;
11+
12+ c . __clyBootstrap = bootstrapMeta ;
13+
14+ function queueCall ( methodPath ) {
15+ return function ( ) {
16+ pendingCalls . push ( [ methodPath ] . concat ( Array . prototype . slice . call ( arguments , 0 ) ) ) ;
17+ return c ;
18+ } ;
19+ }
20+
21+ function addStub ( target , methodName , methodPath ) {
22+ var stub = queueCall ( methodPath || methodName ) ;
23+ stub . __isCountlyStub = true ;
24+ target [ methodName ] = stub ;
25+ }
26+
27+ function invokeMethod ( root , methodPath , args ) {
28+ var chunks = methodPath . split ( "." ) ;
29+ var context = root ;
30+ for ( var idx = 0 ; idx < chunks . length - 1 ; idx ++ ) {
31+ context = context && context [ chunks [ idx ] ] ;
32+ }
33+ var methodName = chunks [ chunks . length - 1 ] ;
34+ var method = context && context [ methodName ] ;
35+ if ( typeof method === "function" && ! method . __isCountlyStub ) {
36+ method . apply ( context , args || [ ] ) ;
37+ }
38+ }
39+
40+ function flushPendingCalls ( ) {
41+ var live = w . Countly ;
42+ var queue = pendingCalls . slice ( ) ;
43+ pendingCalls = [ ] ;
44+ for ( var idx = 0 ; idx < queue . length ; idx ++ ) {
45+ var call = queue [ idx ] ;
46+ invokeMethod ( live , call [ 0 ] , call . slice ( 1 ) ) ;
47+ }
48+ }
49+
50+ function loadSdk ( scriptUrl ) {
51+ if ( isLoaded || isLoading ) {
52+ return ;
53+ }
54+ isLoading = true ;
55+
56+ var script = d . createElement ( "script" ) ;
57+ script . type = "text/javascript" ;
58+ script . async = true ;
59+ script . src = scriptUrl ;
60+ script . crossOrigin = "anonymous" ;
61+ script . onload = function ( ) {
62+ isLoaded = true ;
63+ isLoading = false ;
64+
65+ var live = w . Countly ;
66+ if ( live && typeof live . init === "function" ) {
67+ live . init ( initConfig || { } ) ;
68+ }
69+ flushPendingCalls ( ) ;
70+ } ;
71+ script . onerror = function ( ) {
72+ isLoading = false ;
73+ console . error ( "Countly loader failed to load SDK from:" , scriptUrl ) ;
74+ } ;
75+
76+ var first = d . getElementsByTagName ( "script" ) [ 0 ] ;
77+ first . parentNode . insertBefore ( script , first ) ;
78+ }
79+
80+ var methods = [
81+ "track_sessions" ,
82+ "track_pageview" ,
83+ "track_performance" ,
84+ "add_event" ,
85+ "user_details" ,
86+ "track_errors" ,
87+ "track_forms" ,
88+ "track_clicks" ,
89+ "track_scrolls" ,
90+ "track_links" ,
91+ "recordError" ,
92+ "change_id" ,
93+ "set_id" ,
94+ "uploadUserProfilePicture" ,
95+ "content.enterContentZone" ,
96+ "content.refreshContentZone" ,
97+ "content.exitContentZone" ,
98+ "feedback.showNPS" ,
99+ "feedback.showSurvey" ,
100+ "feedback.showRating" ,
101+ "userData.set" ,
102+ "userData.set_once" ,
103+ "userData.increment" ,
104+ "userData.increment_by" ,
105+ "userData.multiply" ,
106+ "userData.max" ,
107+ "userData.min" ,
108+ "userData.push" ,
109+ "userData.push_unique" ,
110+ "userData.pull" ,
111+ "userData.unset" ,
112+ "userData.save"
113+ ] ;
114+
115+ for ( var i = 0 ; i < methods . length ; i ++ ) {
116+ var parts = methods [ i ] . split ( "." ) ;
117+ if ( parts . length === 2 ) {
118+ c [ parts [ 0 ] ] = c [ parts [ 0 ] ] || { } ;
119+ addStub ( c [ parts [ 0 ] ] , parts [ 1 ] , methods [ i ] ) ;
120+ }
121+ else {
122+ addStub ( c , parts [ 0 ] , methods [ i ] ) ;
123+ }
124+ }
125+
126+ c . init = function ( appKey , config ) {
127+ if ( typeof appKey === "object" ) {
128+ config = appKey || { } ;
129+ appKey = config . app_key ;
130+ }
131+ config = config || { } ;
132+
133+ if ( appKey ) {
134+ config . app_key = appKey ;
135+ }
136+
137+ for ( var key in config ) {
138+ if ( Object . prototype . hasOwnProperty . call ( config , key ) ) {
139+ c [ key ] = config [ key ] ;
140+ }
141+ }
142+
143+ if ( c . app_key === "YOUR_APP_KEY" || c . url === "https://your.server.ly" ) {
144+ console . warn ( "Please do not use default set of app key and server url" ) ;
145+ }
146+
147+ initConfig = config ;
148+ var sdkUrl = c . sdk_url || "https://cdn.jsdelivr.net/npm/countly-sdk-web@latest/lib/countly.min.js" ;
149+ loadSdk ( sdkUrl ) ;
150+ return c ;
151+ } ;
152+ } ) ( window , document ) ;
0 commit comments