@go1/go1-embedding-react-sdk
TypeScript icon, indicating that this package has built-in type declarations

0.0.54 • Public • Published

Go1 Embedding React SDK

A react hook to embed a Go1 content view in your site. Includes two-way messaging between your site and the Go1 content view.

Installation

npm install @go1/go1-embedding-react-sdk

Usage

Import the hook from the library and use it in your functional components.

import { useMemo, useCallback } from 'react';
import { ContentViewProps, useGo1ContentView } from '@go1/go1-embedding-react-sdk';

export function YourHostingComponent() {
  const onGo1MessageReceived = useCallback((message) => {
    console.log('Received postMessage from Go1');
    console.log(message);
  }, []);

  const options = useMemo<ContentViewProps>(() => {
    return {
      feature: 'ai-chat',
      portalURL: 'learning-incorporated-usa.mygo1.com',
      onGo1MessageReceived,
    };
  }, []);

  const { sendMessageToGo1, ContentView } = useGo1ContentView(options);

  return (
    <div style={{ width: '100vw', height: '100vh' }}>
      <ContentView />
    </div>
  );
}

APIs

Types

export type OnGo1MessageReceived = (message: Go1Message) => void;

export type SendMessageToGo1 = (message: Go1Message) => void;

export type IframesMap = { [key: string]: HTMLIFrameElement };

export interface AdditionalInfoMessage {
  additionalUserInfo?: AdditionalUserInfo;
  featureAttributes?: FeatureAttributes;
  themeInformation?: ThemeInformation;
}

export interface ThemeInformation {
  /** Hex string, starting with a #. Example #0437F2 */
  accent: string;
}

export type Go1MessageType =
  | 'preview_start_content'
  | 'preview_close'
  | 'preview_add_content_success'
  | 'preview_add_content_failure'
  | 'preview_remove_content_success'
  | 'preview_remove_content_failure'
  | 'parent_show_preview'
  | 'set_additional_embedding_data'
  | 'go1_app_initialised'
  | 'ott_required'
  | 'search_result_selected'
  | 'search_filter_toggle'
  | 'pass_ott'
  | 'logout',
  | 'sync_content'
  | 'inter_feature_navigation'
  | 'side_drawer_open'
  | 'side_drawer_close';

export interface Go1Message {
  type: Go1MessageType;
  payload?: any;
}

export type Experience = 'manager' | 'learner' | 'context-aware'

interface FeatureAttributeCommon {
  shouldSuppressPreview?: boolean;
  experience?: Experience;
  contentScope?: 'subscription' | 'library';
}

/**
 * Used to configure the `ai-chat` feature
 */
export interface FeatureAttributesAIChat extends FeatureAttributeCommon {
  intent?: 'skill_gap' | 'improve_skills' | 'next_role' | 'aspirations';
  message?: string; // The desired message to start the chat, which will be shown to the user
  existingSkills?: string[]; // max items 7, any more are discarded
  desiredSkills?: string[]; // max items 7, any more are discarded
  desiredRole?: string; // e.g. "Senior Account Manager"
}

/**
 * Used to configure the `preview` feature
 */
export interface FeatureAttributesPreview extends FeatureAttributeCommon {
  id: number;
  startWith1Player?: boolean;
  showFooter?: boolean;
  contentRatingType?: 'no rating' | 'five star rating' | 'like rating';
}

/**
 * Used to configure the `wallet` feature
 */
export interface FeatureAttributesWallet extends FeatureAttributeCommon {
  walletMode: 'setup' | 'main';
  deepLink?: string;
  oneClickBuyUrl?: string;
}

export interface FeatureAttributesLearnerSearch extends FeatureAttributeCommon {
  isGo1MobileApp?: boolean;
  experience?: 'learner';
}

export interface FeatureAttributesLibrary extends FeatureAttributeCommon, FeatureAttributesRetirement {
  libraryEmptyStateDescription?: string;
  shouldShowLibraryTabs?: boolean;
}

export interface FeatureAttributesRetirement extends FeatureAttributeCommon {
  shouldShowAlternativesSearchButton?: boolean;
  shouldShowNotificationEmailRegisterSwitch?: boolean;
}

export interface FeatureAttributesSearch extends FeatureAttributeCommon {}
export interface FeatureAttributesMyPlaylists extends FeatureAttributesPreviewCommon {
  playlistId?: number; // If embedding my-playlists feature with playlistId, the app will initialise with the specified playlistId page.
}

export interface FeatureAttributesMyLearning extends FeatureAttributeCommon {}

export type FeatureAttributes =
  | FeatureAttributesAIChat
  | FeatureAttributesPreview
  | FeatureAttributesWallet
  | FeatureAttributesLibrary
  | FeatureAttributesSearch
  | FeatureAttributesMyPlaylists
  | FeatureAttributesMyLearning;;

export interface AdditionalUserInfo {
  jobTitle: string;
}

export type FeatureType =
  | 'ai-chat'
  | 'content-hub'
  | 'preview'
  | 'library'
  | 'search'
  | 'wallet'
  | 'content-retirement'
  | 'my-playlists'
  | 'activity-feed'
  | 'my-learning';

export interface InitOptions {
  /** Mandatory identifier for the embedded feature */
  feature: FeatureType;
  /** Optional element id to search for, and use as the parent for the Go1 content view (use either this or `iframeParentElement`, not both) */
  iframeParentId?: string;
  /** Optional element to use as the parent for the Go1 content view (use either this or `iframeParentId`, not both) */
  iframeParentElement?: HTMLElement;
  /** Optional parameters to configure the feature. See associated types for examples */
  featureAttributes?: FeatureAttributes;
  /** Optional Go1 portal URL, used for login purposes if no `oneTimeToken` is provided */
  portalURL?: string;
  /** Optional function that is invoked when the Go1 content view emits a message */
  onGo1MessageReceived?: OnGo1MessageReceived;
  /** Optional end user information that can enhance the context and provide a better experience. No PII is needed, please do not send any. */
  additionalUserInfo?: AdditionalUserInfo;
  /** Optional UI configuration */
  themeInformation?: ThemeInformation;
  /** Optional token that is used to pre-authorize the user without them needing to log in */
  oneTimeToken?: string;
  /** Optional Id that let us know which integration system users are coming from (will be required in the future)*/
  integrationSystemId?: string;
  /** Optional Id that let us know which integration system users are coming from, if using an intermediary system to embed */
  presentingIntegrationSystemId?: string;
}

Function: useGo1ContentView

A react hook that will return

  • sendMessageToGo1: A function that allows you to send a message to the Go1 content view
  • ContentView: A react component to render the Go1 content view
function useGo1ContentView(options: InitOptions): {
  sendMessageToGo1: SendMessageToGo1;
  ContentView: () => React.JSX.Element;
};

Usage

const onGo1MessageReceived = useCallback((message) => {
  console.log('Received postMessage from Go1');
  console.log(message);
}, []);

const options = useMemo<ContentViewProps>(() => {
  return {
    feature: 'ai-chat',
    portalURL: 'learning-incorporated-usa.mygo1.com',
    featureAttributes: {
      experience: 'manager',
    },
    onGo1MessageReceived,
    additionalUserInfo: {
      jobTitle: 'Sales development representative',
    },
    themeInformation: {
      accent: '#0437F2',
    },
    oneTimeToken: '907687a6e81a458190fe4c21f05ee689',
    integrationSystemId: 'int_nI7rPykBRm0C',
  };
}, []);

const { sendMessageToGo1, ContentView } = useGo1ContentView(options);

// now to render the <ContentView>

Function: sendMessageToGo1

Send a message to the Go1 content view. Useful to control the user experience when an event starts on your website and the Go1 content view needs to be aware of it (and potentially perform other actions - this is feature dependent.)

A currently supported message is update_experience, this will update the experience Feature Attribute. An example of its usage is below.

function sendMessageToGo1(message: Go1Message): void;

Usage

const message = {
  type: 'update_experience',
  payload: { experience: 'learner' },
};
sendMessageToGo1(message);

Function: onGo1MessageReceived

Respond to events that have occurred in the Go1 content view. Useful to control the user experience when an event starts in the Go1 content view and your website needs to be aware of it.

Usage

const onGo1MessageReceived = (message) => {
  switch (message.type) {
    case 'enrolment_created': {
      const { userId, courseId } = message.payload;
      // perform appropriate actions for when a new enrolment is created
      break;
    }
    case 'category_favourite_added': {
      const { userId, category } = message.payload;
      // perform appropriate actions for when a new category is favourited
      break;
    }
    default:
      break;
  }
};

Support

https://www.go1.com/developers

Package Sidebar

Install

npm i @go1/go1-embedding-react-sdk

Weekly Downloads

732

Version

0.0.54

License

ISC

Unpacked Size

28.4 kB

Total Files

11

Last publish

Collaborators

  • andrew_hood_go1
  • lukebrooker
  • univate