Skip to content

Commit

Permalink
refactor: pf login mixpanel events
Browse files Browse the repository at this point in the history
  • Loading branch information
alessandrogelmi committed Mar 28, 2024
1 parent 307da5c commit 3eb5b73
Show file tree
Hide file tree
Showing 17 changed files with 261 additions and 77 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export enum PFLoginEventsType {
SEND_LOGIN = 'SEND_LOGIN',
SEND_IDP_SELECTED = 'SEND_IDP_SELECTED',
SEND_LOGIN_FAILURE = 'SEND_LOGIN_FAILURE',
SEND_LOGIN_METHOD = 'SEND_LOGIN_METHOD',
}
9 changes: 5 additions & 4 deletions packages/pn-personafisica-login/src/pages/login/Login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import { styled } from '@mui/material/styles';
import { AppRouteParams, Layout, useIsMobile } from '@pagopa-pn/pn-commons';
import { CieIcon, SpidIcon } from '@pagopa/mui-italia/dist/icons';

import { PFLoginEventsType } from '../../models/PFLoginEventsType';
import { getConfiguration } from '../../services/configuration.service';
import { TrackEventType } from '../../utility/events';
import { trackEventByType } from '../../utility/mixpanel';
import PFLoginEventStrategyFactory from '../../utility/MixpanelUtils/PFLoginEventStrategyFactory';
import { storageAarOps } from '../../utility/storage';
import SpidSelect from './SpidSelect';

Expand All @@ -38,14 +38,15 @@ const Login = () => {
}

useEffect(() => {
trackEventByType(TrackEventType.SEND_LOGIN);
PFLoginEventStrategyFactory.triggerEvent(PFLoginEventsType.SEND_LOGIN);
}, []);

const goCIE = () => {
window.location.assign(
`${URL_API_LOGIN}/login?entityID=${SPID_CIE_ENTITY_ID}&authLevel=SpidL2&RelayState=send`
);
trackEventByType(TrackEventType.SEND_IDP_SELECTED, {

PFLoginEventStrategyFactory.triggerEvent(PFLoginEventsType.SEND_IDP_SELECTED, {
SPID_IDP_NAME: 'CIE',
SPID_IDP_ID: SPID_CIE_ENTITY_ID,
});
Expand Down
15 changes: 6 additions & 9 deletions packages/pn-personafisica-login/src/pages/login/SpidSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ import Grid from '@mui/material/Grid';
import Icon from '@mui/material/Icon';
import Link from '@mui/material/Link';
import Typography from '@mui/material/Typography';
import { ProfilePropertyType } from '@pagopa-pn/pn-commons';

import SpidBig from '../../assets/spid_big.svg';
import { PFLoginEventsType } from '../../models/PFLoginEventsType';
import { getConfiguration } from '../../services/configuration.service';
import { IdentityProvider, getIDPS } from '../../utility/IDPS';
import { TrackEventType } from '../../utility/events';
import { setSuperOrProfilePropertyValues, trackEventByType } from '../../utility/mixpanel';
import PFLoginEventStrategyFactory from '../../utility/MixpanelUtils/PFLoginEventStrategyFactory';
import { shuffleList } from '../../utility/utils';

const SpidSelect = ({ onBack }: { onBack: () => void }) => {
Expand All @@ -25,16 +24,14 @@ const SpidSelect = ({ onBack }: { onBack: () => void }) => {
const getSPID = (IDP: IdentityProvider) => {
sessionStorage.setItem('IDP', IDP.entityId);

trackEventByType(TrackEventType.SEND_IDP_SELECTED, {
PFLoginEventStrategyFactory.triggerEvent(PFLoginEventsType.SEND_IDP_SELECTED, {
SPID_IDP_NAME: IDP.name,
SPID_IDP_ID: IDP.entityId,
});

setSuperOrProfilePropertyValues(
ProfilePropertyType.PROFILE,
'SEND_LOGIN_METHOD',
IDP.entityId as any // FIX this any
);
PFLoginEventStrategyFactory.triggerEvent(PFLoginEventsType.SEND_LOGIN_METHOD, {
entityID: IDP.entityId,
});

window.location.assign(
`${URL_API_LOGIN}/login?entityID=${IDP.entityId}&authLevel=SpidL2&RelayState=send`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@ import { Box, Button, Dialog, Typography } from '@mui/material';
import { getLocalizedOrDefaultLabel } from '@pagopa-pn/pn-commons/src/utility/localization.utility';
import { IllusError } from '@pagopa/mui-italia';

import { PFLoginEventsType } from '../../models/PFLoginEventsType';
import { getConfiguration } from '../../services/configuration.service';
import { TrackEventType } from '../../utility/events';
import { trackEventByType } from '../../utility/mixpanel';
import PFLoginEventStrategyFactory from '../../utility/MixpanelUtils/PFLoginEventStrategyFactory';

const handleError = (queryParams: string, errorMessage: string) => {
if (process.env.NODE_ENV !== 'test') {
const IDP = sessionStorage.getItem('IDP');
trackEventByType(TrackEventType.SEND_LOGIN_FAILURE, { reason: errorMessage, IDP });
PFLoginEventStrategyFactory.triggerEvent(PFLoginEventsType.SEND_LOGIN_FAILURE, {
reason: errorMessage,
IDP,
});
sessionStorage.removeItem('IDP');
console.error(`login unsuccessfull! query params obtained from idp: ${queryParams}`);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { EventStrategy, EventStrategyFactory } from '@pagopa-pn/pn-commons';

import { PFLoginEventsType } from '../../models/PFLoginEventsType';
import { SendIDPSelectedStrategy } from './Strategies/SendIDPSelectedStrategy';
import { SendLoginFailureStrategy } from './Strategies/SendLoginFailureStrategy';
import { SendLoginMethodStrategy } from './Strategies/SendLoginMethodStrategy';
import { UXScreenViewStrategy } from './Strategies/UXScreenViewStrategy';

class PFLoginEventStrategyFactory extends EventStrategyFactory<PFLoginEventsType> {
getStrategy(eventType: PFLoginEventsType): EventStrategy | null {
switch (eventType) {
case PFLoginEventsType.SEND_LOGIN:
return new UXScreenViewStrategy();
case PFLoginEventsType.SEND_IDP_SELECTED:
return new SendIDPSelectedStrategy();
case PFLoginEventsType.SEND_LOGIN_FAILURE:
return new SendLoginFailureStrategy();
case PFLoginEventsType.SEND_LOGIN_METHOD:
return new SendLoginMethodStrategy();
default:
return null;
}
}
}

export default new PFLoginEventStrategyFactory();
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import {
EventAction,
EventCategory,
EventPropertyType,
EventStrategy,
TrackedEvent,
} from '@pagopa-pn/pn-commons';

type SendIDPSelected = {
SPID_IDP_NAME: string;
SPID_IDP_ID: string;
};

export class SendIDPSelectedStrategy implements EventStrategy {
performComputations({
SPID_IDP_ID,
SPID_IDP_NAME,
}: SendIDPSelected): TrackedEvent<SendIDPSelected> {
return {
[EventPropertyType.TRACK]: {
event_category: EventCategory.UX,
event_type: EventAction.ACTION,
SPID_IDP_ID,
SPID_IDP_NAME,
},
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {
EventCategory,
EventPropertyType,
EventStrategy,
TrackedEvent,
} from '@pagopa-pn/pn-commons';

type SendLoginFailure = {
reason: string;
IDP: string | null;
};

export class SendLoginFailureStrategy implements EventStrategy {
performComputations({ reason, IDP }: SendLoginFailure): TrackedEvent<SendLoginFailure> {
return {
[EventPropertyType.TRACK]: {
event_category: EventCategory.TECH,
reason,
IDP,
},
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { EventPropertyType, EventStrategy, TrackedEvent } from '@pagopa-pn/pn-commons';

type SendLoginMethod = {
entityID:
| 'cie'
| 'posteid'
| 'timid'
| 'spiditalia'
| 'sielteid'
| 'namirialid'
| 'lepidaid'
| 'instesaid'
| 'infocertid'
| 'arubaid';
};

type SendLoginMethodReturn = {
SEND_LOGIN_METHOD: string;
};

export class SendLoginMethodStrategy implements EventStrategy {
performComputations({ entityID }: SendLoginMethod): TrackedEvent<SendLoginMethodReturn> {
return {
[EventPropertyType.PROFILE]: {
SEND_LOGIN_METHOD: entityID,
},
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {
EventAction,
EventCategory,
EventPropertyType,
EventStrategy,
TrackedEvent,
} from '@pagopa-pn/pn-commons';

export class UXScreenViewStrategy implements EventStrategy {
performComputations(): TrackedEvent {
return {
[EventPropertyType.TRACK]: {
event_category: EventCategory.UX,
event_type: EventAction.SCREEN_VIEW,
},
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { EventAction, EventCategory, EventPropertyType } from '@pagopa-pn/pn-commons';

import { SendIDPSelectedStrategy } from '../SendIDPSelectedStrategy';

describe('Mixpanel - Send IDP Selected Strategy', () => {
it('should return IDP selected event', () => {
const strategy = new SendIDPSelectedStrategy();
const event = strategy.performComputations({
SPID_IDP_ID: 'idp_id',
SPID_IDP_NAME: 'idp_name',
});

expect(event).toEqual({
[EventPropertyType.TRACK]: {
event_category: EventCategory.UX,
event_type: EventAction.ACTION,
SPID_IDP_ID: 'idp_id',
SPID_IDP_NAME: 'idp_name',
},
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { EventCategory, EventPropertyType } from '@pagopa-pn/pn-commons';

import { SendLoginFailureStrategy } from '../SendLoginFailureStrategy';

describe('Mixpanel - Send Login Failure Strategy', () => {
it('should return login failure event', () => {
const strategy = new SendLoginFailureStrategy();
const event = strategy.performComputations({
reason: 'error',
IDP: 'test',
});

expect(event).toEqual({
[EventPropertyType.TRACK]: {
event_category: EventCategory.TECH,
reason: 'error',
IDP: 'test',
},
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { EventPropertyType } from '@pagopa-pn/pn-commons';

import { SendLoginMethodStrategy } from '../SendLoginMethodStrategy';

describe('Mixpanel - Send Login Method Strategy', () => {
it('should return login method event', () => {
const strategy = new SendLoginMethodStrategy();
const event = strategy.performComputations({
entityID: 'cie',
});

expect(event).toEqual({
[EventPropertyType.PROFILE]: {
SEND_LOGIN_METHOD: 'cie',
},
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { EventAction, EventCategory, EventPropertyType } from '@pagopa-pn/pn-commons';

import { UXScreenViewStrategy } from '../UXScreenViewStrategy';

describe('Mixpanel - UX Screen View Strategy', () => {
it('should return UX screen view event', () => {
const strategy = new UXScreenViewStrategy();

const uxScreenViewEvent = strategy.performComputations();
expect(uxScreenViewEvent).toEqual({
[EventPropertyType.TRACK]: {
event_category: EventCategory.UX,
event_type: EventAction.SCREEN_VIEW,
},
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { PFLoginEventsType } from '../../../models/PFLoginEventsType';
import PFLoginEventStrategyFactory from '../PFLoginEventStrategyFactory';
import { SendIDPSelectedStrategy } from '../Strategies/SendIDPSelectedStrategy';
import { SendLoginFailureStrategy } from '../Strategies/SendLoginFailureStrategy';
import { SendLoginMethodStrategy } from '../Strategies/SendLoginMethodStrategy';
import { UXScreenViewStrategy } from '../Strategies/UXScreenViewStrategy';

describe('Event Strategy Factory', () => {
const factory = PFLoginEventStrategyFactory;

it('should return UXScreenViewStrategy for SEND_LOGIN event', () => {
expect(factory.getStrategy(PFLoginEventsType.SEND_LOGIN)).toBeInstanceOf(UXScreenViewStrategy);
});

it('should return SendIDPSelectedStrategy for SEND_IDP_SELECTED event', () => {
expect(factory.getStrategy(PFLoginEventsType.SEND_IDP_SELECTED)).toBeInstanceOf(
SendIDPSelectedStrategy
);
});

it('should return SendLoginFailureStrategy for SEND_LOGIN_FAILURE event', () => {
expect(factory.getStrategy(PFLoginEventsType.SEND_LOGIN_FAILURE)).toBeInstanceOf(
SendLoginFailureStrategy
);
});

it('should return SendLoginMethodStrategy for SEND_LOGIN_METHOD event', () => {
expect(factory.getStrategy(PFLoginEventsType.SEND_LOGIN_METHOD)).toBeInstanceOf(
SendLoginMethodStrategy
);
});

it('should return null for unknown event type', () => {
expect(factory.getStrategy('UNKNOWN_EVENT' as PFLoginEventsType)).toBeNull();
});
});
21 changes: 0 additions & 21 deletions packages/pn-personafisica-login/src/utility/events.ts

This file was deleted.

27 changes: 0 additions & 27 deletions packages/pn-personafisica-login/src/utility/mixpanel.ts

This file was deleted.

Loading

0 comments on commit 3eb5b73

Please sign in to comment.