Skip to content

Commit

Permalink
[MINI-6189] - Send Analytics to Miniapp (#258)
Browse files Browse the repository at this point in the history
* Initial Commit

* Pettier fix

* Clean

* Prettier fix

* Feedback comments

* Refactor
  • Loading branch information
rleojoseph authored Jun 29, 2023
1 parent 250d73c commit 97ef669
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 7 deletions.
34 changes: 29 additions & 5 deletions js-miniapp-bridge/src/common-bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { MiniAppError, parseMiniAppError } from './types/error-types';
import { MiniAppResponseInfo } from './types/response-types/miniapp';
import { ProductInfo, PurchasedProductInfo } from './types/in-app-purchase';
import { HostThemeColor } from './types/host-color-scheme';
import { MAAnalytics } from './types/analytics/analytics';

/** @internal */
const mabMessageQueue: Callback[] = [];
Expand Down Expand Up @@ -759,11 +760,20 @@ export class MiniAppBridge {
'isDarkMode',
null,
response => {
if (response.toLowerCase() === 'true') {
resolve(Boolean(true));
} else {
resolve(Boolean(false));
}
resolve(BooleanValue(response));
},
error => reject(parseMiniAppError(error))
);
});
}

sendAnalytics(analyticsInfo: MAAnalytics) {
return new Promise<boolean>((resolve, reject) => {
return this.executor.exec(
'sendAnalytics',
analyticsInfo,
response => {
resolve(BooleanValue(response));
},
error => reject(parseMiniAppError(error))
);
Expand Down Expand Up @@ -811,3 +821,17 @@ function trimBannerText(message: string = null, maxLength = 128) {
? message?.substring(0, maxLength - 1) + '…'
: message;
}

function BooleanValue(value) {
if (typeof value === 'boolean') {
return value;
} else if (typeof value === 'string') {
const lowerCaseValue = value.toLowerCase();
if (lowerCaseValue === 'true' || lowerCaseValue === '1') {
return true;
} else if (lowerCaseValue === 'false' || lowerCaseValue === '0') {
return false;
}
}
return false;
}
2 changes: 2 additions & 0 deletions js-miniapp-bridge/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { Platform } from './types/platform';
import { MessageToContact } from './types/message-to-contact';
import { CloseAlertInfo } from './types/close-alert';
import { HostThemeColor } from './types/host-color-scheme';
import { MAAnalytics } from './types/analytics/analytics';

import {
AuthorizationFailureError,
Expand Down Expand Up @@ -101,4 +102,5 @@ export {
ProductPurchasedAlreadyError,
UserCancelledPurchaseError,
HostThemeColor,
MAAnalytics,
};
32 changes: 32 additions & 0 deletions js-miniapp-bridge/src/types/analytics/analytics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Analytics Event type
*/
export enum MAAnalyticsEventType {
appear,
click,
error,
custom,
}

/**
* Action Type
*/
export enum MAAnalyticsActionType {
open,
close,
add,
delete,
change,
}

/**
* Mini App Analytics info type
*/
export interface MAAnalytics {
eventType: MAAnalyticsEventType;
actionType: MAAnalyticsActionType;
pageName: string;
componentName: string;
elementType: string;
data: string;
}
2 changes: 1 addition & 1 deletion js-miniapp-bridge/test/test.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ describe('shareInfo', () => {
describe('getUserName', () => {
it('will return the close status string response', () => {
const bridge = new Bridge.MiniAppBridge(mockExecutor);
const response = 'leo';
const response = 'miniapp';
mockExecutor.exec.callsArgWith(2, response);

return expect(bridge.getUserName()).to.eventually.deep.equal(response);
Expand Down
15 changes: 14 additions & 1 deletion js-miniapp-sdk/src/modules/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { getBridge } from '../sdkbridge';
import { CloseAlertInfo, HostThemeColor } from '../../../js-miniapp-bridge/src';
import {
CloseAlertInfo,
HostThemeColor,
MAAnalytics,
} from '../../../js-miniapp-bridge/src';

/**
* Mini App Utility methods
Expand Down Expand Up @@ -27,6 +31,12 @@ export interface MiniAppUtilsProvider {
* Interface to check if the Device/Application is using Dark mode
*/
isDarkMode(): Promise<boolean>;

/**
* Interface to send analytics to Host app
* @param analyticsInfo Analytics info
*/
sendAnalytics(analyticsInfo: MAAnalytics): Promise<boolean>;
}

/** @internal */
Expand All @@ -43,4 +53,7 @@ export class MiniAppUtils implements MiniAppUtilsProvider {
isDarkMode(): Promise<boolean> {
return getBridge().isDarkMode();
}
sendAnalytics(analyticsInfo: MAAnalytics): Promise<boolean> {
return getBridge().sendAnalytics(analyticsInfo);
}
}
7 changes: 7 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1445,6 +1445,13 @@
version "5.11.15"
resolved "https://registry.yarnpkg.com/@mui/core-downloads-tracker/-/core-downloads-tracker-5.11.15.tgz#a85cc60d8c1104bf1963229dea17744f674eb1f3"

"@mui/icons-material@^5.11.16":
version "5.11.16"
resolved "https://registry.yarnpkg.com/@mui/icons-material/-/icons-material-5.11.16.tgz#417fa773c56672e39d6ccfed9ac55591985f0d38"
integrity sha512-oKkx9z9Kwg40NtcIajF9uOXhxiyTZrrm9nmIJ4UjkU2IdHpd4QVLbCc/5hZN/y0C6qzi2Zlxyr9TGddQx2vx2A==
dependencies:
"@babel/runtime" "^7.21.0"

"@mui/material@^5.10.17":
version "5.11.15"
resolved "https://registry.yarnpkg.com/@mui/material/-/material-5.11.15.tgz#101afbc5399aee3cd551eb066bf375df448fa263"
Expand Down

0 comments on commit 97ef669

Please sign in to comment.