-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding support for exposing ready on analytics object
update dependencies
- Loading branch information
1 parent
296c9a7
commit 66f2744
Showing
15 changed files
with
478 additions
and
266 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,8 @@ import Storage from "./utils/storage"; | |
import { EventRepository } from "./utils/EventRepository"; | ||
import logger from "./utils/logUtil"; | ||
import { addDomEventHandlers } from "./utils/autotrack.js"; | ||
import Emitter from "component-emitter"; | ||
import after from "after"; | ||
|
||
//https://unpkg.com/[email protected]/dist/browser.js | ||
|
||
|
@@ -44,7 +46,6 @@ class Analytics { | |
this.autoTrackHandlersRegistered = false; | ||
this.autoTrackFeatureEnabled = false; | ||
this.initialized = false; | ||
this.ready = false; | ||
this.trackValues = []; | ||
this.eventsBuffer = []; | ||
this.clientIntegrations = []; | ||
|
@@ -66,6 +67,8 @@ class Analytics { | |
this.anonymousId = this.getAnonymousId(); | ||
this.storage.setUserId(this.userId); | ||
this.eventRepository = EventRepository; | ||
this.readyCallback = undefined; | ||
this.executeReadyCallback = undefined; | ||
} | ||
|
||
/** | ||
|
@@ -96,7 +99,7 @@ class Analytics { | |
" Use Native SDK? " + | ||
destination.config.useNativeSDK | ||
); | ||
if (destination.enabled && destination.config.useNativeSDK) { | ||
if (destination.enabled) { | ||
this.clientIntegrations.push(destination.destinationDefinition.name); | ||
this.configArray.push(destination.config); | ||
} | ||
|
@@ -130,6 +133,9 @@ class Analytics { | |
this.clientIntegrationObjects = []; | ||
|
||
if (!intgArray || intgArray.length == 0) { | ||
if (this.readyCallback) { | ||
this.readyCallback(); | ||
} | ||
this.toBeProcessedByIntegrationArray = []; | ||
return; | ||
} | ||
|
@@ -152,6 +158,20 @@ class Analytics { | |
object.clientIntegrations.length | ||
) { | ||
object.clientIntegrationObjects = object.successfullyLoadedIntegration; | ||
|
||
object.executeReadyCallback = after( | ||
object.clientIntegrationObjects.length, | ||
object.readyCallback | ||
); | ||
|
||
object.on("ready", object.executeReadyCallback); | ||
|
||
object.clientIntegrationObjects.forEach(intg => { | ||
if (!intg["isReady"] || intg["isReady"]()) { | ||
object.emit("ready"); | ||
} | ||
}); | ||
|
||
//send the queued events to the fetched integration | ||
object.toBeProcessedByIntegrationArray.forEach(event => { | ||
let methodName = event[0]; | ||
|
@@ -604,6 +624,14 @@ class Analytics { | |
} | ||
} | ||
} | ||
|
||
ready(callback) { | ||
if (typeof callback == "function") { | ||
this.readyCallback = callback; | ||
return; | ||
} | ||
logger.error("ready callback is not a function"); | ||
} | ||
} | ||
|
||
if (process.browser) { | ||
|
@@ -618,6 +646,8 @@ if (process.browser) { | |
|
||
let instance = new Analytics(); | ||
|
||
Emitter(instance); | ||
|
||
if (process.browser) { | ||
let eventsPushedAlready = | ||
!!window.rudderanalytics && | ||
|
@@ -645,6 +675,7 @@ if (process.browser) { | |
} | ||
} | ||
|
||
let ready = instance.ready.bind(instance); | ||
let identify = instance.identify.bind(instance); | ||
let page = instance.page.bind(instance); | ||
let track = instance.track.bind(instance); | ||
|
@@ -657,6 +688,7 @@ let setAnonymousId = instance.setAnonymousId.bind(instance); | |
|
||
export { | ||
initialized, | ||
ready, | ||
page, | ||
track, | ||
load, | ||
|
Oops, something went wrong.