FacebookImpl.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import { AppTracks } from "@scripts/app/AppTrack";
  2. export interface IFBLoginRes {
  3. accessToken: string;
  4. /** SDK 返回的用户资料,仅用于展示;服务端仍需验证登录凭证。 */
  5. userId?: string;
  6. name?: string;
  7. /** iOS Limited Login JWT; never use as a Graph API access token. */
  8. authenticationToken?: string;
  9. tokenType?: "access" | "authentication";
  10. nonce?: string;
  11. isCancel: boolean;
  12. status?: string;
  13. reason?: string;
  14. }
  15. export type IFBEventValue = string | number;
  16. export type IFBEventParams = Record<string, IFBEventValue>;
  17. export interface IFBShareOptions {
  18. url: string;
  19. hashtag?: string;
  20. quote?: string;
  21. }
  22. export interface IFBShareRes {
  23. success: boolean;
  24. isCancel: boolean;
  25. postId?: string;
  26. reason?: string;
  27. }
  28. const FBEventNames = {
  29. AchievedLevel: "fb_mobile_level_achieved",
  30. ActivatedApp: "fb_mobile_activate_app",
  31. AddedPaymentInfo: "fb_mobile_add_payment_info",
  32. AddedToCart: "fb_mobile_add_to_cart",
  33. AddedToWishlist: "fb_mobile_add_to_wishlist",
  34. CompletedRegistration: "fb_mobile_complete_registration",
  35. CompletedTutorial: "fb_mobile_tutorial_completion",
  36. InitiatedCheckout: "fb_mobile_initiated_checkout",
  37. Purchased: "fb_mobile_purchase",
  38. Rated: "fb_mobile_rate",
  39. Searched: "fb_mobile_search",
  40. SpentCredits: "fb_mobile_spent_credits",
  41. UnlockedAchievement: "fb_mobile_achievement_unlocked",
  42. ViewedContent: "fb_mobile_content_view",
  43. }
  44. const FBEventParameterName = {
  45. ContentID: "fb_content_id",
  46. ContentType: "fb_content_type",
  47. Currency: "fb_currency",
  48. Description: "fb_description",
  49. Level: "fb_level",
  50. MaxRatingValue: "fb_max_rating_value",
  51. NumItems: "fb_num_items",
  52. PaymentInfoAvailable: "fb_payment_info_available",
  53. RegistrationMethod: "fb_registration_method",
  54. SearchString: "fb_search_string",
  55. Success: "fb_success",
  56. }
  57. const EventsMap = {
  58. [AppTracks.Registered]: FBEventNames.CompletedRegistration,
  59. [AppTracks.AddToWishlist]: FBEventNames.AddedToWishlist,
  60. [AppTracks.Addtocart]: FBEventNames.AddedToCart,
  61. }
  62. export abstract class FacebookImpl {
  63. private _isInitialized = false;
  64. public get isInitialized() { return this._isInitialized; }
  65. abstract init(): void;
  66. protected onInited() {
  67. this._isInitialized = true;
  68. this.onLogEvent(FBEventNames.ActivatedApp);
  69. }
  70. async login(): Promise<IFBLoginRes> {
  71. const res = await this.onLogin();
  72. return res;
  73. }
  74. protected abstract onLogin(): Promise<IFBLoginRes>;
  75. abstract logOut(): Promise<boolean>;
  76. abstract share(options: IFBShareOptions): Promise<IFBShareRes>;
  77. logEvent(eventName: string, parameters?: IFBEventParams, valueToSum?: number): Promise<boolean> {
  78. const fbEvent = EventsMap[eventName]
  79. if (!!fbEvent) {
  80. return this.onLogEvent(fbEvent, parameters, valueToSum);
  81. }
  82. if (eventName === AppTracks.Purchased) {
  83. let currency = parameters?.currency || "";
  84. if (typeof currency === 'number') currency = currency.toString();
  85. const value = Number(parameters?.value || valueToSum);
  86. this.logPurchase(value, currency, {
  87. [FBEventParameterName.Currency]: currency
  88. })
  89. }
  90. }
  91. abstract onLogEvent(eventName: string, parameters?: IFBEventParams, valueToSum?: number): Promise<boolean>;
  92. abstract logPurchase(
  93. amount: number,
  94. currency: string,
  95. parameters?: IFBEventParams,
  96. ): Promise<boolean>;
  97. abstract flushEvents(): Promise<boolean>;
  98. }