import ApiClient from '.'; import { AnalyticsQueryParams, ContentEngagementSummary, ContentEngagementSummaryResponse, ContentEngagementByPresentation, ContentEngagementByPresentationResponse, ContentEngagementByClick, ContentEngagementByClickResponse, DayTraffic, DayTrafficResponse, EngagementByClick, EngagementByClickResponse, EngagementByLocation, EngagementByLocationResponse, EngagementByProduct, EngagementByProductResponse, TrafficSummary, TrafficSummaryResponse, GetAnalytics, GetAnalyticsRequest, GetAnalyticsResponse, GetContentEngagement, GetContentEngagementResponse, GetAnalyticsLocationsRequest, GetAnalyticsLocationsResponse, GetTrafficAwareness, GetTrafficAwarenessRequest, GetTrafficAwarenessResponse, LocationTraffic, LocationTrafficResponse, TrafficAwarenessQueryParams, WeekdayHourTraffic, WeekdayHourTrafficResponse, InteractionResponse, Interaction, CSVText, ContentDetailQueryParams, GetContentDetailResponse, GetContentDetail, ContentDetailSummaryResponse, ContentDetailSummary, ContentDetailByPlaylistResponse, ContentDetailByPlaylist, ContentDetailByLocationResponse, ContentDetailByLocation, ContentDetailByDayResponse, ContentDetailByDay, } from './types/Analytics'; import urls from './urls'; const mapEngagementByLocationResponse = ({ location_id: locationId, location_name: locationName, pickup_to_screen_conversion: pickupToScreenConversion, total_interactions: totalInteractions, ...others }: EngagementByLocationResponse): EngagementByLocation => ({ locationId, locationName, pickupToScreenConversion, totalInteractions, ...others, }); const mapEngagementByProductResponse = ({ pickup_to_screen_conversion: pickupToScreenConversion, product_category_id: productCategoryId, product_category_name: productCategoryName, total_interactions: totalInteractions, ...others }: EngagementByProductResponse): EngagementByProduct => ({ pickupToScreenConversion, productCategoryId, productCategoryName, totalInteractions, ...others, }); const mapEngagementByClickResponse = ({ page_title: pageTitle, page_type: pageType, percent_clicks_on_page: percentClicksOnPage, presentation_id: presentationId, presentation_name: presentationName, interaction_source: interactionSource, ...others }: EngagementByClickResponse): EngagementByClick => ({ pageTitle, pageType, percentClicksOnPage, presentationId, presentationName, interactionSource, ...others, }); const mapGetAnalyticsResponse = ({ engagement_by_click, engagement_by_day: engagementByDay, engagement_by_day_previous_period: engagementByDayPreviousPeriod, engagement_by_location, engagement_by_product, }: GetAnalyticsResponse): GetAnalytics => ({ engagementByClick: engagement_by_click?.map(mapEngagementByClickResponse), engagementByDay, engagementByDayPreviousPeriod, engagementByLocation: engagement_by_location?.map( mapEngagementByLocationResponse, ), engagementByProduct: engagement_by_product?.map( mapEngagementByProductResponse, ), }); const mapContentEngagementSummaryResponse = ({ dwell_duration: dwellDuration, avg_dwell_duration: avgDwellDuration, ots_face: otsFace, dwell_face_duration: dwellFaceDuration, avg_dwell_face_duration: avgDwellFaceDuration, verified_views: verifiedViews, avg_verified_views_per_day: avgVerifiedViewsPerDay, avg_verified_views_per_week: avgVerifiedViewsPerWeek, view_duration: viewDuration, avg_view_duration: avgViewDuration, avg_impressions_per_day: avgImpressionsPerDay, avg_impressions_per_week: avgImpressionsPerWeek, engaged_views: engagedViews, avg_engaged_views_per_day: avgEngagedViewsPerDay, avg_engaged_views_per_week: avgEngagedViewsPerWeek, verified_views_rate: verifiedViewsRate, engaged_views_rate: engagedViewsRate, ...others }: ContentEngagementSummaryResponse): ContentEngagementSummary => ({ dwellDuration, avgDwellDuration, otsFace, dwellFaceDuration, avgDwellFaceDuration, verifiedViews, avgVerifiedViewsPerDay, avgVerifiedViewsPerWeek, viewDuration, avgViewDuration, avgImpressionsPerDay, avgImpressionsPerWeek, engagedViews, avgEngagedViewsPerDay, avgEngagedViewsPerWeek, verifiedViewsRate, engagedViewsRate, ...others, }); const mapContentEngagementByPresentationResponse = ({ presentation_id: presentationId, presentation_name: presentationName, n_of_plays: nOfPlays, avg_duration: avgDuration, playback_duration: playbackDuration, n_of_locations: nOfLocations, last_played_time: lastPlayedTime, playback_distribution: playbackDistribution, dwell_duration: dwellDuration, avg_dwell_duration: avgDwellDuration, ots_face: otsFace, dwell_face_duration: dwellFaceDuration, avg_dwell_face_duration: avgDwellFaceDuration, verified_views: verifiedViews, view_duration: viewDuration, avg_view_duration: avgViewDuration, verified_views_rate: verifiedViewsRate, engaged_views: engagedViews, engaged_views_rate: engagedViewsRate, ...others }: ContentEngagementByPresentationResponse): ContentEngagementByPresentation => ({ presentationId, presentationName, nOfPlays, avgDuration, playbackDuration, nOfLocations, lastPlayedTime, playbackDistribution, dwellDuration, avgDwellDuration, otsFace, dwellFaceDuration, avgDwellFaceDuration, verifiedViews, viewDuration, avgViewDuration, verifiedViewsRate, engagedViews, engagedViewsRate, ...others, }); const mapContentEngagementByClickResponse = ({ presentation_id: presentationId, presentation_name: presentationName, interaction_source: interactionSource, page_type: pageType, page_title: pageTitle, percent_clicks_on_page: percentClicksOnPage, ...others }: ContentEngagementByClickResponse): ContentEngagementByClick => ({ presentationId, presentationName, interactionSource, pageType, pageTitle, percentClicksOnPage, ...others, }); const mapGetContentEngagementesponse = ({ content_engagement_summary, content_engagement_summary_previous_period, content_engagement_by_presentation, content_engagement_by_click, }: GetContentEngagementResponse): GetContentEngagement => ({ contentEngagementSummary: content_engagement_summary?.map( mapContentEngagementSummaryResponse, ), contentEngagementSummaryPreviousPeriod: content_engagement_summary_previous_period?.map( mapContentEngagementSummaryResponse, ), contentEngagementByPresentation: content_engagement_by_presentation?.map( mapContentEngagementByPresentationResponse, ), contentEngagementByClick: content_engagement_by_click?.map( mapContentEngagementByClickResponse, ), }); const mapTrafficSummaryResponse = ({ ots: ots, avg_ots_per_day: avgOtsPerDay, avg_ots_per_week: avgOtsPerWeek, dwell_time: dwellTime, avg_dwell_time: avgDwellTime, ots_face: otsFace, avg_ots_face_per_day: avgOtsFacePerDay, avg_ots_face_per_week: avgOtsFacePerWeek, dwell_time_face: dwellTimeFace, avg_dwell_time_face: avgDwellTimeFace, screen_placement_quality: screenPlacementQuality, impressions: impressions, avg_impressions_per_day: avgImpressionsPerDay, avg_impressions_per_week: avgImpressionsPerWeek, verified_views: verifiedViews, avg_verified_views_per_day: avgVerifiedViewsPerDay, avg_verified_views_per_week: avgVerifiedViewsPerWeek, verified_views_rate: verifiedViewsRate, engaged_views: engagedViews, avg_engaged_views_per_day: avgEngagedViewsPerDay, avg_engaged_views_per_week: avgEngagedViewsPerWeek, engaged_views_rate: engagedViewsRate, ...others }: TrafficSummaryResponse): TrafficSummary => ({ ots, avgOtsPerDay, avgOtsPerWeek, dwellTime, avgDwellTime, otsFace, avgOtsFacePerDay, avgOtsFacePerWeek, dwellTimeFace, avgDwellTimeFace, screenPlacementQuality, impressions, avgImpressionsPerDay, avgImpressionsPerWeek, verifiedViews, avgVerifiedViewsPerDay, avgVerifiedViewsPerWeek, verifiedViewsRate, engagedViews, avgEngagedViewsPerDay, avgEngagedViewsPerWeek, engagedViewsRate, ...others, }); const mapTrafficByDayResponse = ({ dwell_time: dwellTime, avg_dwell_time: avgDwellTime, ots_face: otsFace, dwell_time_face: dwellTimeFace, avg_dwell_time_face: avgDwellTimeFace, verified_views: verifiedViews, engaged_views: engagedViews, ...others }: DayTrafficResponse): DayTraffic => ({ dwellTime, avgDwellTime, otsFace, dwellTimeFace, avgDwellTimeFace, verifiedViews, engagedViews, ...others, }); const mapTrafficByLocationResponse = ({ location_id: locationId, location_name: locationName, dwell_time: dwellTime, avg_dwell_time: avgDwellTime, ots_face: otsFace, dwell_time_face: dwellTimeFace, avg_dwell_time_face: avgDwellTimeFace, verified_views: verifiedViews, engaged_views: engagedViews, screen_placement_quality: screenPlacementQuality, verified_views_rate: verifiedViewsRate, avg_viewing_time: avgViewingTime, ...others }: LocationTrafficResponse): LocationTraffic => ({ locationId, locationName, dwellTime, avgDwellTime, otsFace, dwellTimeFace, avgDwellTimeFace, verifiedViews, engagedViews, screenPlacementQuality, verifiedViewsRate, avgViewingTime, ...others, }); const mapTrafficByWeekdayHourResponse = ({ dwell_time: dwellTime, avg_dwell_time: avgDwellTime, ots_face: otsFace, dwell_time_face: dwellTimeFace, avg_dwell_time_face: avgDwellTimeFace, verified_views: verifiedViews, engaged_views: engagedViews, ...others }: WeekdayHourTrafficResponse): WeekdayHourTraffic => ({ dwellTime, avgDwellTime, otsFace, dwellTimeFace, avgDwellTimeFace, verifiedViews, engagedViews, ...others, }); const mapInteractionsResponse = ({ total_interactions: totalInteractions, ...others }: InteractionResponse): Interaction => ({ totalInteractions, ...others, }); const mapGetTrafficAwarenessResponse = ({ traffic_summary, traffic_summary_previous_period, traffic_by_day, traffic_by_location, traffic_by_weekday_hour, interactions, }: GetTrafficAwarenessResponse): GetTrafficAwareness => ({ trafficSummary: traffic_summary?.map(mapTrafficSummaryResponse), trafficSummaryPreviousPeriod: traffic_summary_previous_period?.map( mapTrafficSummaryResponse, ), trafficByDay: traffic_by_day?.map(mapTrafficByDayResponse), trafficByLocation: traffic_by_location?.map(mapTrafficByLocationResponse), trafficByWeekdayHour: traffic_by_weekday_hour?.map( mapTrafficByWeekdayHourResponse, ), interactions: interactions?.map(mapInteractionsResponse), }); const mapContentDetailSummaryResponse = ({ presentation_id: presentationId, presentation_name: presentationName, n_of_plays: nOfPlays, playback_duration: playbackDuration, n_of_locations: nOfLocations, n_of_playlists: nOfPlaylists, n_of_screens: nOfScreens, n_of_days: nOfDays, first_time_played: firstTimePlayed, last_time_played: lastTimePlayed, dwell_duration: dwellDuration, avg_dwell_duration: avgDwellDuration, all_avg_dwell_duration: allAvgDwellDuration, verified_views: verifiedViews, all_avg_verified_views: allAvgVerifiedViews, view_duration: viewDuration, avg_view_duration: avgViewDuration, all_avg_view_duration: allAvgViewDuration, all_avg_impressions: allAvgImpressions, screen_placement_quality: screenPlacementQuality, all_screen_placement_quality: allScreenPlacementQuality, engaged_views: engagedViews, all_avg_engaged_views: allAvgEngagedViews, verified_views_rate: verifiedViewsRate, all_avg_verified_views_rate: allAvgVerifiedViewsRate, engaged_views_rate: engagedViewsRate, all_avg_engaged_views_rate: allAvgEngagedViewsRate, verified_views_female: verifiedViewsFemale, verified_views_female_percentage: verifiedViewsFemalePercentage, avg_viewing_duration_female: avgViewingDurationFemale, verified_view_rate_female: verifiedViewRateFemale, verified_views_male: verifiedViewsMale, verified_views_male_percentage: verifiedViewsMalePercentage, avg_viewing_duration_male: avgViewingDurationMale, verified_view_rate_male: verifiedViewRateMale, ...others }: ContentDetailSummaryResponse): ContentDetailSummary => ({ presentationId, presentationName, nOfPlays, playbackDuration, nOfLocations, nOfPlaylists, nOfScreens, nOfDays, firstTimePlayed, lastTimePlayed, dwellDuration, avgDwellDuration, allAvgDwellDuration, verifiedViews, allAvgVerifiedViews, viewDuration, avgViewDuration, allAvgViewDuration, allAvgImpressions, screenPlacementQuality, allScreenPlacementQuality, engagedViews, allAvgEngagedViews, verifiedViewsRate, allAvgVerifiedViewsRate, engagedViewsRate, allAvgEngagedViewsRate, verifiedViewsFemale, verifiedViewsFemalePercentage, avgViewingDurationFemale, verifiedViewRateFemale, verifiedViewsMale, verifiedViewsMalePercentage, avgViewingDurationMale, verifiedViewRateMale, ...others, }); const mapContentDetailByPlaylistResponse = ({ playlist_id: playlistId, playlist_name: playlistName, n_of_plays: nOfPlays, playback_duration: playbackDuration, n_of_locations: nOfLocations, first_time_played: firstTimePlayed, last_time_played: lastTimePlayed, dwell_duration: dwellDuration, avg_dwell_duration: avgDwellDuration, verified_views: verifiedViews, view_duration: viewDuration, avg_view_duration: avgViewDuration, engaged_views: engagedViews, verified_views_rate: verifiedViewsRate, engaged_views_rate: engagedViewsRate, ...others }: ContentDetailByPlaylistResponse): ContentDetailByPlaylist => ({ playlistId, playlistName, nOfPlays, playbackDuration, nOfLocations, firstTimePlayed, lastTimePlayed, dwellDuration, avgDwellDuration, verifiedViews, viewDuration, avgViewDuration, engagedViews, verifiedViewsRate, engagedViewsRate, ...others, }); const mapContentDetailByLocationResponse = ({ location_id: locationId, location_name: locationName, n_of_plays: nOfPlays, playback_duration: playbackDuration, first_played_time: firstPlayedTime, last_played_time: lastPlayedTime, dwell_duration: dwellDuration, avg_dwell_duration: avgDwellDuration, verified_views: verifiedViews, view_duration: viewDuration, avg_view_duration: avgViewDuration, engaged_views: engagedViews, verified_views_rate: verifiedViewsRate, engaged_views_rate: engagedViewsRate, ...others }: ContentDetailByLocationResponse): ContentDetailByLocation => ({ locationId, locationName, nOfPlays, playbackDuration, firstPlayedTime, lastPlayedTime, dwellDuration, avgDwellDuration, verifiedViews, viewDuration, avgViewDuration, engagedViews, verifiedViewsRate, engagedViewsRate, ...others, }); const mapContentDetailByDayResponse = ({ timestamp: timestamp, n_of_plays: nOfPlays, playback_duration: playbackDuration, dwell_duration: dwellDuration, avg_dwell_duration: avgDwellDuration, verified_views: verifiedViews, view_duration: viewDuration, avg_view_duration: avgViewDuration, engaged_views: engagedViews, verified_views_rate: verifiedViewsRate, engaged_views_rate: engagedViewsRate, ...others }: ContentDetailByDayResponse): ContentDetailByDay => ({ timestamp, nOfPlays, playbackDuration, dwellDuration, avgDwellDuration, verifiedViews, viewDuration, avgViewDuration, engagedViews, verifiedViewsRate, engagedViewsRate, ...others, }); const mapGetContentDetailResponse = ({ content_engagement_detail_summary, content_engagement_detail_by_playlist, content_engagement_detail_by_location, content_engagement_detail_by_day, }: GetContentDetailResponse): GetContentDetail => ({ contentDetailSummary: content_engagement_detail_summary?.map( mapContentDetailSummaryResponse, ), contentDetailByPlaylist: content_engagement_detail_by_playlist?.map( mapContentDetailByPlaylistResponse, ), contentDetailByLocation: content_engagement_detail_by_location?.map( mapContentDetailByLocationResponse, ), contentDetailByDay: content_engagement_detail_by_day?.map( mapContentDetailByDayResponse, ), }); export default class AnalyticsClient { async getAnalytics( this: ApiClient, query: AnalyticsQueryParams, ): Promise { const data = await this.requestProtected< GetAnalyticsRequest, GetAnalyticsResponse >({ method: 'GET', url: urls.getAnalytics(query), }); return mapGetAnalyticsResponse(data); } async getLocations(this: ApiClient): Promise { return this.requestProtected< GetAnalyticsLocationsRequest, GetAnalyticsLocationsResponse >({ method: 'GET', url: urls.getLocations(), }); } async getContentEngagement( this: ApiClient, query: AnalyticsQueryParams, ): Promise { const data = await this.requestProtected< GetAnalyticsRequest, GetContentEngagementResponse >({ method: 'GET', url: urls.getContentEngagement(query), }); return mapGetContentEngagementesponse(data); } async getTrafficAwareness( this: ApiClient, query: AnalyticsQueryParams, ): Promise { const trafficAwarenessQuery = { ...query, location_id: [query.location_id], } as TrafficAwarenessQueryParams; const data = await this.requestProtected< GetTrafficAwarenessRequest, GetTrafficAwarenessResponse >({ method: 'GET', url: urls.getTrafficAwareness(trafficAwarenessQuery), }); return mapGetTrafficAwarenessResponse(data); } async getTrafficAwarenessExport( this: ApiClient, query: AnalyticsQueryParams, ): Promise { const trafficAwarenessQuery = { ...query, location_id: [query.location_id], } as TrafficAwarenessQueryParams; const data = await this.requestProtected({ method: 'GET', url: urls.getTrafficAwarenessExport(trafficAwarenessQuery), }); return data; } async getContentEngagementExport( this: ApiClient, query: AnalyticsQueryParams, ): Promise { const data = await this.requestProtected({ method: 'GET', url: urls.getContentEngagementExport(query), responseType: 'blob', }); return data; } async getContentEngagementClickExport( this: ApiClient, query: AnalyticsQueryParams, ): Promise { const data = await this.requestProtected({ method: 'GET', url: urls.getContentEngagementClickExport(query), }); return data; } async getProductEngagementExport( this: ApiClient, query: AnalyticsQueryParams, ): Promise { const data = await this.requestProtected({ method: 'GET', url: urls.getProductEngagementExport(query), }); return data; } async getContentDetail( this: ApiClient, query: ContentDetailQueryParams, ): Promise { const data = await this.requestProtected< GetAnalyticsRequest, GetContentDetailResponse >({ method: 'GET', url: urls.getContentDetail(query), }); return mapGetContentDetailResponse(data); } }