Skip to content

Commit

Permalink
Tag PID to BQ delivery metric (#7788)
Browse files Browse the repository at this point in the history
* Tag PID to BQ delivery metric

* Make linter happy

* Run format &  add changeset

* Update stupid-humans-fry.md
  • Loading branch information
zwu52 committed Nov 16, 2023
1 parent b2163b3 commit b782bb2
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/stupid-humans-fry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@firebase/messaging': patch
---

Tag product id for internal big query metric logging.
1 change: 1 addition & 0 deletions packages/logger/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
Expand Down
12 changes: 9 additions & 3 deletions packages/messaging/src/helpers/externalizePayload.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ describe('externalizePayload', () => {
// eslint-disable-next-line camelcase
collapse_key: 'collapse',
// eslint-disable-next-line camelcase
fcmMessageId: 'mid'
fcmMessageId: 'mid',
// eslint-disable-next-line camelcase
productId: 123
};

const payload: MessagePayload = {
Expand Down Expand Up @@ -66,7 +68,9 @@ describe('externalizePayload', () => {
// eslint-disable-next-line camelcase
collapse_key: 'collapse',
// eslint-disable-next-line camelcase
fcmMessageId: 'mid'
fcmMessageId: 'mid',
// eslint-disable-next-line camelcase
productId: 123
};

const payload: MessagePayload = {
Expand Down Expand Up @@ -100,7 +104,9 @@ describe('externalizePayload', () => {
// eslint-disable-next-line camelcase
collapse_key: 'collapse',
// eslint-disable-next-line camelcase
fcmMessageId: 'mid'
fcmMessageId: 'mid',
// eslint-disable-next-line camelcase
productId: 123
};

const payload: MessagePayload = {
Expand Down
24 changes: 21 additions & 3 deletions packages/messaging/src/helpers/logToFirelog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ import {
FcmEvent,
LogEvent,
LogRequest,
LogResponse
LogResponse,
ComplianceData
} from '../interfaces/logging-types';

import { MessagePayloadInternal } from '../interfaces/internal-message-payload';
Expand Down Expand Up @@ -162,7 +163,7 @@ export async function stageLog(
await messaging.firebaseDependencies.installations.getId()
);

createAndEnqueueLogEvent(messaging, fcmEvent);
createAndEnqueueLogEvent(messaging, fcmEvent, internalPayload.productId);
}

function createFcmEvent(
Expand Down Expand Up @@ -208,18 +209,35 @@ function createFcmEvent(

function createAndEnqueueLogEvent(
messaging: MessagingService,
fcmEvent: FcmEvent
fcmEvent: FcmEvent,
productId: number
): void {
const logEvent = {} as LogEvent;

/* eslint-disable camelcase */
logEvent.event_time_ms = Math.floor(Date.now()).toString();
logEvent.source_extension_json_proto3 = JSON.stringify(fcmEvent);

if (!!productId) {
logEvent.compliance_data = buildComplianceData(productId);
}
// eslint-disable-next-line camelcase

messaging.logEvents.push(logEvent);
}

function buildComplianceData(productId: number): ComplianceData {
const complianceData: ComplianceData = {
privacy_context: {
prequest: {
origin_associated_product_id: productId
}
}
};

return complianceData;
}

export function _createLogRequest(logEventQueue: LogEvent[]): LogRequest {
const logRequest = {} as LogRequest;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export interface MessagePayloadInternal {
isFirebaseMessaging?: boolean;
from: string;
fcmMessageId: string;
productId: number;
// eslint-disable-next-line camelcase
collapse_key: string;
}
Expand Down
15 changes: 15 additions & 0 deletions packages/messaging/src/interfaces/logging-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,21 @@ export interface LogEvent {

// A stringified json proto version of source_extension.
source_extension_json_proto3: string;

// The compliance data associated with this event.
compliance_data: ComplianceData;
}

export interface ComplianceData {
privacy_context: ExternalPrivacyContext;
}

export interface ExternalPrivacyContext {
prequest: ExternalPRequestContext;
}

export interface ExternalPRequestContext {
origin_associated_product_id: number;
}
/* eslint-enable camelcase */

Expand Down
3 changes: 2 additions & 1 deletion packages/messaging/src/listeners/sw-listeners.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ const DISPLAY_MESSAGE: MessagePayloadInternal = {
// eslint-disable-next-line camelcase
collapse_key: 'collapse',
// eslint-disable-next-line camelcase
fcmMessageId: 'mid'
fcmMessageId: 'mid',
productId: 123
};

// maxActions is an experimental property and not part of the official
Expand Down
20 changes: 18 additions & 2 deletions packages/messaging/src/testing/fakes/logging-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,16 @@ import {
FcmEvent,
LogEvent,
LogResponse,
UserResponse
UserResponse,
ComplianceData
} from '../../interfaces/logging-types';

export function getFakeLogEvent(): LogEvent {
return {
/* eslint-disable camelcase */
event_time_ms: '0',
source_extension_json_proto3: JSON.stringify(getFakeFcmEvent())
source_extension_json_proto3: JSON.stringify(getFakeFcmEvent()),
compliance_data: getFakeComplianceData()
/* eslint-enable camelcase */
};
}
Expand All @@ -50,6 +52,20 @@ function getFakeFcmEvent(): FcmEvent {
};
}

function getFakeComplianceData(): ComplianceData {
const fakeComplianceData: ComplianceData = {
/* eslint-disable camelcase */
privacy_context: {
prequest: {
origin_associated_product_id: 123
}
}
/* eslint-enable camelcase */
};

return fakeComplianceData;
}

export function getSuccessResponse(): LogResponse {
return Object.create({
nextRequestWaitMillis: 1000,
Expand Down

0 comments on commit b782bb2

Please sign in to comment.