@@ -17,7 +17,7 @@ import LegacyManager from './managers/LegacyManager';
1717import SdkEnvironment from './managers/SdkEnvironment' ;
1818import { AppConfig , AppUserConfig , AppUserConfigNotifyButton } from './models/AppConfig' ;
1919import Context from './models/Context' ;
20- import { Notification } from ' ./models/Notification' ;
20+ import { Notification } from " ./models/Notification" ;
2121import { NotificationActionButton } from './models/NotificationActionButton' ;
2222import { NotificationPermission } from './models/NotificationPermission' ;
2323import { WindowEnvironmentKind } from './models/WindowEnvironmentKind' ;
@@ -53,6 +53,9 @@ import { ProcessOneSignalPushCalls } from "./utils/ProcessOneSignalPushCalls";
5353import { AutoPromptOptions } from "./managers/PromptsManager" ;
5454import { EnvironmentInfoHelper } from './context/browser/helpers/EnvironmentInfoHelper' ;
5555import { EnvironmentInfo } from './context/browser/models/EnvironmentInfo' ;
56+ import { SessionManager } from './managers/sessionManager/page/SessionManager' ;
57+ import OutcomesHelper from "./helpers/shared/OutcomesHelper" ;
58+ import { OutcomeAttributionType } from "./models/Outcomes" ;
5659
5760export default class OneSignal {
5861 /**
@@ -204,6 +207,11 @@ export default class OneSignal {
204207 const appConfig = await new ConfigManager ( ) . getAppConfig ( options ) ;
205208 Log . debug ( `OneSignal: Final web app config: %c${ JSON . stringify ( appConfig , null , 4 ) } ` , getConsoleStyle ( 'code' ) ) ;
206209
210+ // TODO: environmentInfo is explicitly dependent on existence of OneSignal.config. Needs refactor.
211+ // Workaround to temp assign config so that it can be used in context.
212+ OneSignal . config = appConfig ;
213+ OneSignal . environmentInfo = EnvironmentInfoHelper . getEnvironmentInfo ( ) ;
214+
207215 OneSignal . context = new Context ( appConfig ) ;
208216 OneSignal . config = OneSignal . context . appConfig ;
209217 }
@@ -219,8 +227,6 @@ export default class OneSignal {
219227 InitHelper . errorIfInitAlreadyCalled ( ) ;
220228 await OneSignal . initializeConfig ( options ) ;
221229
222- OneSignal . environmentInfo = EnvironmentInfoHelper . getEnvironmentInfo ( ) ;
223-
224230 if ( ! OneSignal . config ) {
225231 throw new Error ( "OneSignal config not initialized!" ) ;
226232 }
@@ -272,6 +278,16 @@ export default class OneSignal {
272278 */
273279 if ( ! OneSignal . config || ! OneSignal . config . subdomain )
274280 throw new SdkInitError ( SdkInitErrorKind . MissingSubdomain ) ;
281+
282+ /**
283+ * We'll need to set up page activity tracking events on the main page but we can do so
284+ * only after the main initialization in the iframe is successful and a new session
285+ * is initiated.
286+ */
287+ OneSignal . emitter . on (
288+ OneSignal . EVENTS . SESSION_STARTED , SessionManager . setupSessionEventListenersForHttp
289+ ) ;
290+
275291 /**
276292 * The iFrame may never load (e.g. OneSignal might be down), in which
277293 * case the rest of the SDK's initialization will be blocked. This is a
@@ -453,7 +469,7 @@ export default class OneSignal {
453469 }
454470 // After the user subscribers, he will have a device ID, so get it again
455471 var { deviceId : newDeviceId } = await Database . getSubscription ( ) ;
456- await OneSignalApi . updatePlayer ( appId , newDeviceId , {
472+ await OneSignalApi . updatePlayer ( appId , newDeviceId ! , {
457473 tags : tags
458474 } ) ;
459475 executeCallback ( callback , tags ) ;
@@ -781,6 +797,53 @@ export default class OneSignal {
781797 return this . emitter . once ( event , listener ) ;
782798 }
783799
800+ public static async sendOutcome ( outcomeName : string , outcomeWeight ?: number | undefined ) : Promise < void > {
801+ const outcomesConfig = OneSignal . config ! . userConfig . outcomes ;
802+ if ( ! outcomesConfig ) {
803+ Log . debug ( "Outcomes feature not supported by main application yet." ) ;
804+ return ;
805+ }
806+ if ( ! outcomeName ) {
807+ Log . error ( "Outcome name is required" ) ;
808+ return ;
809+ }
810+ if ( typeof outcomeWeight !== "undefined" && typeof outcomeWeight !== "number" ) {
811+ Log . error ( "Outcome weight can only be a number if present." ) ;
812+ return ;
813+ }
814+ // TODO: check built-in outcome names? not allow sending?
815+
816+ await awaitOneSignalInitAndSupported ( ) ;
817+
818+ const isSubscribed = await OneSignal . privateIsPushNotificationsEnabled ( ) ;
819+ if ( ! isSubscribed ) {
820+ Log . warn ( "Reporting outcomes is supported only for subscribed users." ) ;
821+ return ;
822+ }
823+
824+ // TODO: add error handling
825+ const outcomeAttribution = await OutcomesHelper . getAttribution ( outcomesConfig ) ;
826+ switch ( outcomeAttribution . type ) {
827+ case OutcomeAttributionType . Direct :
828+ await OneSignal . context . updateManager . sendOutcomeDirect (
829+ OneSignal . config ! . appId , outcomeAttribution . notificationIds , outcomeName , outcomeWeight
830+ ) ;
831+ return ;
832+ case OutcomeAttributionType . Indirect :
833+ await OneSignal . context . updateManager . sendOutcomeInfluenced (
834+ OneSignal . config ! . appId , outcomeAttribution . notificationIds , outcomeName , outcomeWeight
835+ ) ;
836+ return ;
837+ case OutcomeAttributionType . Unattributed :
838+ await OneSignal . context . updateManager . sendOutcomeUnattributed (
839+ OneSignal . config ! . appId , outcomeName , outcomeWeight ) ;
840+ return ;
841+ default :
842+ Log . warn ( "You are on a free plan. Please upgrade to use this functionality." ) ;
843+ return ;
844+ }
845+ }
846+
784847 static __doNotShowWelcomeNotification : boolean ;
785848 static VERSION = Environment . version ( ) ;
786849 static _VERSION = Environment . version ( ) ;
@@ -825,6 +888,7 @@ export default class OneSignal {
825888 static proxyFrameHost : ProxyFrameHost ;
826889 static proxyFrame : ProxyFrame ;
827890 static emitter : Emitter = new Emitter ( ) ;
891+ static cache : any = { } ;
828892
829893 /**
830894 * The additional path to the worker file.
@@ -874,6 +938,7 @@ export default class OneSignal {
874938 CONNECTED : 'connect' ,
875939 REMOTE_NOTIFICATION_PERMISSION : 'postmam.remoteNotificationPermission' ,
876940 REMOTE_DATABASE_GET : 'postmam.remoteDatabaseGet' ,
941+ REMOTE_DATABASE_GET_ALL : 'postmam.remoteDatabaseGetAll' ,
877942 REMOTE_DATABASE_PUT : 'postmam.remoteDatabasePut' ,
878943 REMOTE_DATABASE_REMOVE : 'postmam.remoteDatabaseRemove' ,
879944 REMOTE_OPERATION_COMPLETE : 'postman.operationComplete' ,
@@ -904,6 +969,10 @@ export default class OneSignal {
904969 SUBSCRIPTION_EXPIRATION_STATE : 'postmam.subscriptionExpirationState' ,
905970 PROCESS_EXPIRING_SUBSCRIPTIONS : 'postmam.processExpiringSubscriptions' ,
906971 GET_SUBSCRIPTION_STATE : 'postmam.getSubscriptionState' ,
972+ SESSION_UPSERT : 'postmam.sessionUpsert' ,
973+ SESSION_DEACTIVATE : 'postmam.sessionDeactivate' ,
974+ ARE_YOU_VISIBLE_REQUEST : 'postmam.areYouVisibleRequest' ,
975+ ARE_YOU_VISIBLE_RESPONSE : 'postmam.areYouVisibleResponse' ,
907976 } ;
908977
909978 static EVENTS = {
@@ -973,6 +1042,7 @@ export default class OneSignal {
9731042 TEST_INIT_OPTION_DISABLED : 'testInitOptionDisabled' ,
9741043 TEST_WOULD_DISPLAY : 'testWouldDisplay' ,
9751044 POPUP_WINDOW_TIMEOUT : 'popupWindowTimeout' ,
1045+ SESSION_STARTED : "os.sessionStarted" ,
9761046 } ;
9771047}
9781048
0 commit comments