import * as prettySize from 'prettysize' ; import httpService from '../../../../src/app/core/services/http.service' ; import { UserSettings } from 'app/auth/types' ; import localStorageService from 'app/core/services/local-storage.service' ; import { DevicePlatform , SignupDeviceSource } from 'app/core/types' ; import { DriveItemData } from 'app/drive/types' ; import { AnalyticsTrack , PriceData } from '../types' ; import queryString from 'query-string' ; export const PATH_NAMES = { '/new' : 'Register' , '/appsumo' : 'Register' , '/login' : 'Login' , '/storage' : 'Drive Main' , '/settings' : 'drive-web-settings' , '/invite' : 'drive-web-invite' , '/remove' : 'drive-web-remove' , } ; export function trackFileDownloadCompleted ( properties ) { trackData ( properties , 'file_downloaded' ) ; } function trackData ( properties , actionName ) { const user = localStorageService . getUser ( ) ; httpService . post ( `${ process . env . REACT_APP_API_URL }/api/data` , { actionName , user , properties , } ) ; } const payload = { usage : 0 , limit : 0 , plan : 0 , } ; export function page ( pageName ) { window . analytics . page ( pageName ) ; } export function signupDevicesource ( userAgent ) { for ( const device in SignupDeviceSource ) { if ( new RegExp ( device ) . test ( userAgent ) ) { return device ; } } return SignupDeviceSource . Other ; } export function signupCampaignSource ( locationSearch ) { const parameters = new URLSearchParams ( locationSearch ) ; const partner = parameters . get ( 'internxt_partner' ) ; const campaign = parameters . get ( 'utm_campaign' ) ; const impact = parameters . get ( 'irclickid' ) ? 'impact' : null ; const other = 'organic' ; const source = [ partner , campaign , impact , other ] . find ( ( o ) => typeof o !== 'undefined' && o !== null ) ; return source ; } function getUser ( ) { return localStorageService . getUser ( ) as UserSettings ; } function identifyPlanName ( bytes ) { return bytes === 0 ? 'Free 2GB' : prettySize ( bytes ) ; } export function identifyUsage ( newValue ) { if ( newValue !== payload . usage ) { payload . usage = newValue ; window . analytics . identify ( getUser ( ) . uuid , { userId : getUser ( ) . uuid , storage : newValue , platform : 'web' } ) ; } } export function identifyPlan ( newValue ) { if ( newValue !== payload . plan ) { payload . plan = newValue ; window . analytics . identify ( getUser ( ) . uuid , { userId : getUser ( ) . uuid , plan : identifyPlanName ( newValue ) , platform : 'web' , } ) ; } } export function trackSignOut ( ) { window . analytics . track ( AnalyticsTrack . SignOut , { email : getUser ( ) ?. email , } ) ; } export function trackSignIn ( payload ) { window . analytics . track ( AnalyticsTrack . SignIn , payload ) ; } export function signInAttempted ( email , error ) { window . analytics . track ( AnalyticsTrack . SignInAttempted , { status : 'error' , msg : error ? error : 'Login error' , email : email , } ) ; } export function trackSignUp ( payload ) { window . analytics . identify ( payload . userId , payload . traits ) ; window . analytics . track ( AnalyticsTrack . SignUp , payload . properties ) ; window . rdt ( 'track' , 'SignUp' ) ; } export function trackUserEnterPayments ( priceId ) { window . analytics . track ( AnalyticsTrack . UserEnterPayments , { price_id : priceId } ) ; } export function trackPlanSubscriptionSelected ( payload ) { window . analytics . track ( AnalyticsTrack . PlanSubscriptionSelected , payload ) ; } export function trackFolderCreated ( payload ) { window . analytics . track ( AnalyticsTrack . FolderCreated , payload ) ; } export function trackFolderRename ( payload ) { window . analytics . track ( AnalyticsTrack . FolderRename , payload ) ; } export function trackFileRename ( payload ) { window . analytics . track ( AnalyticsTrack . FileRename , payload ) ; } export function trackFileDownloadStart ( payload ) { window . analytics . track ( AnalyticsTrack . FileDownloadStart , payload ) ; } export function trackFileDownloadError ( payload ) { window . analytics . track ( AnalyticsTrack . FileDownloadError , payload ) ; } export function trackFileDownloadFinished ( payload ) { window . analytics . track ( AnalyticsTrack . FileDownloadFinished , payload ) ; } export function trackFileUploadStart ( payload ) { window . analytics . track ( AnalyticsTrack . FileUploadStart , payload ) ; } export function trackFileUploadError ( payload ) { window . analytics . track ( AnalyticsTrack . FileUploadError , payload ) ; } export function trackFileUploadFinished ( payload ) { window . analytics . track ( AnalyticsTrack . FileUploadFinished , payload ) ; } export function trackMoveItem ( keyOp , payload ) { window . analytics . track ( `${ keyOp }-move` . toLowerCase ( ) , payload ) ; } export function trackDeleteItem ( itemToDelete , payload ) { window . analytics . track ( `${ itemToDelete . isFolder ? 'folder' : 'file' }-delete` , payload ) ; } export function trackOpenWelcomeFile ( ) { window . analytics . track ( AnalyticsTrack . OpenWelcomeFile ) ; } export function trackDeleteWelcomeFile ( ) { window . analytics . track ( AnalyticsTrack . DeleteWelcomeFile ) ; } export function trackFileShare ( ) { window . analytics . track ( AnalyticsTrack . FileShare ) ; } export function identify ( user , email ) { window . analytics . identify ( user . uuid , { email , platform : DevicePlatform . Web , referrals_credit : user . credit , referrals_count : Math . floor ( user . credit / 5 ) , createdAt : user . createdAt , } ) ; } export function trackUserResetPasswordRequest ( ) { window . analytics . track ( AnalyticsTrack . UserResetPasswordRequest ) ; } export function track ( email , status ) { window . analytics . track ( 'user-change-password' , { status , email , } ) ; } export function trackFileUploadBucketIdUndefined ( payload ) { window . analytics . track ( AnalyticsTrack . FileUploadBucketIdUndefined , payload ) ; } export function trackShareLinkBucketIdUndefined ( payload ) { window . analytics . track ( AnalyticsTrack . ShareLinkBucketIdUndefined , payload ) ; } export async function trackPaymentConversion ( ) { window . analytics . page ( 'Checkout Success' ) ; const queryStringParsed = queryString . parse ( location . search ) ; const priceId = String ( queryStringParsed . price_id ) ; const priceData = await httpService . get ( `${ process . env . REACT_APP_API_URL }/api/price` , { params : { priceId } } ) ; const { username , uuid } = getUser ( ) ; window . analytics . identify ( uuid , { email : username , plan : priceId , storage_limit : priceData . metadata . maxSpaceBytes , plan_name : priceData . metadata . name } ) ; window . analytics . track ( AnalyticsTrack . PaymentConversionEvent , { price_id : priceId , email : username , currency : priceData . currency . toUpperCase ( ) , value : priceData . unit_amount * 0.01 , type : priceData . type , plan_name : priceData . metadata . name } ) ; } const analyticsService = { page , identify , identifyUsage , identifyPlan , trackSignOut , trackSignIn , trackSignUp , trackUserEnterPayments , trackPlanSubscriptionSelected , trackFolderCreated , trackFolderRename , trackFileRename , trackFileDownloadStart , trackFileDownloadError , trackFileDownloadFinished , trackFileUploadStart , trackFileUploadError , trackFileUploadFinished , trackMoveItem , trackDeleteItem , trackOpenWelcomeFile , trackDeleteWelcomeFile , trackFileShare , signInAttempted , trackUserResetPasswordRequest , track , trackFileUploadBucketIdUndefined , trackFileDownloadCompleted , trackPaymentConversion , } ; export default analyticsService ;