@buddy-technology/offer-component

中文文档
1.7.1发布于 2026/03/26UNLICENSEDTypeScriptESM + CJS零依赖近 30 天下载 2016

--- React component for adding Buddy's Insurance Gateway

原始使用文档
文档目录21 节
  1. Table of Contents
  2. Requirements
  3. Installation
  4. Basic Usage
  5. Props
  6. Configuration Files
  7. Version 2 configuration files (Recommended)
  8. Version 1 configuration files
  9. View Types
  10. Checkout
  11. For client-side only apps
  12. For full stack apps
  13. Pre-filling Data
  14. Styling
  15. Color Palettes
  16. Overrides
  17. Classes and Components
  18. Capturing data
  19. Quick example
  20. Event types and data fired:
  21. Google Analytics example

Buddy Offer Component


React component for adding Buddy's Insurance Gateway

Table of Contents

Requirements

  • React 16.8.0+
  • Contact Buddy to obtain a partnerID and compatible ions for your specific use case.

Installation

Install it from npm and include it in your React build process

npm install @buddy-technology/offer-component or yarn add @buddy-technology/offer-component


Basic Usage

import React from 'react';
import BuddyOfferElement from '@buddy-technology/offer-component';

function App() {
  return (
    <div id="app">
      <h1>My App</h1>
      <BuddyOfferElement ion="MY_ION" partnerID="my-partner-id" stage="STAGING" />
    </div>
  );
}
export default App;

Props

Option NameTypeDefaultDescription
*partnerIDstringThe partnerID assigned to you from Buddy
*ionstringThe ion ID for the product to be presented
stageenum"STAGING"One of: "STAGING", "PRODUCTION". Set to "STAGING" (default) for testing and development.
configSrcstringThe URL for pairing a configuration file with the offer element.
viewTypeenum"paginated"Sets how the offer element is displayed. One of: "paginated", "single-form", "offer-only". See View types for details.
dataobjectCustomer or policy data to pre-load the offer with. Refer to specific ION documentation for the data structure.
themeobjectThe theme object for passing in styles and color palettes
onUserEventfunctionA callback for tracking user behavioral data. See Capturing Data for details.
includeCheckoutbooleantrueToggles whether to render the secure checkout.
onOptInfunctionThe callback fired when a user opts into an offer. See Buddy's Partner API docs for details on how to complete the transaction.
onOptOutfunctionThe callback fired when a user opts out of the offer.

*required

Configuration Files

For version 2 configuration files (that utilize Buddy.embedOptions), simply pass in the URL of the file to the configSrc prop. Properties defined in a configuration file will be merged with any props defined in this implementation.

EXAMPLE

// config.js 

const configTheme = {
  baseTheme: 'base',
  palette: 'base',
  overrides: {
    styles: {
      h1: { color: 'blue' }
      h2: { color: 'blue' }
    }
  },
};

window.Buddy.embedOptions.theme = configTheme // attach our theme to embedOptions.

// React implementation

import React from 'react';
import BuddyOfferElement from '@buddy-technology/offer-component';

function App() {
  const overrideConfigTheme = {
    overrides: {
      styles: {
        h2: { color: 'red' }
      }
    }
  };

  return (
    <div id="app">
      <h1>My App</h1>
      <BuddyOfferElement
        ion="MY_ION"
        partnerID="my-partner-id"
        stage="staging"
        configSrc="https://js.buddy.insure/config.js" // URL of our config file
        theme={overrideConfigTheme} // deep merges our theme, h2's will now be red, but h1's are still blue.
      />
    </div>
  );
}
export default App;

Version 1 configuration files

NOTE: This feature is useful pairing configuration files utilizing the config variable pattern. For information on how to implement newer configs see: Version 2 Configuration Files.

For configuration files utilizing the config variable pattern, we need to first pre-load our script using the useConfig hook, then pass our options to the implementation.

import React from 'react';
import BuddyOfferElement, { useConfig } from '@buddy-technology/offer-component';

function App() {
  const { config, isLoading } = useConfig('https://config-domain.com/config.js');

  if (isLoading || !config) {
    return null;
  }

  // grab relevant properties from config
  const theme = config.themeBase;
  const data = config.data;
  const handleUserEvents = config.userEvents;

  return (
    <div id="app">
      <h1>My App</h1>
      <BuddyOfferElement
        ion="MY_ION"
        partnerID="my-partner-id"
        stage="staging"
        theme={theme}
        data={data}
        onUserEvent={handleUserEvents}
      />
    </div>
  );
}
export default App;

View Types

Buddy's Offer Element comes in three different view types:

  • paginated (default) - A paginated form where users click through to the next section.
  • single form - A single form where all fields are displayed in the same, scrollable view.
  • offer only - View for displaying just the insurance offer with a quote and checkbox that fires onOptIn when checked and onOptOut when unchecked (see For full stack apps for more info). NOTE: Offer-Only implementations are meant to be rendered in a controlled environment with necessary data passed into it, and will require a payment integration for checkout (see Buddy's Partner API docs). If any fields are missing or invalid, a step-through form will render with the individual views that include the needed fields. Views with satisfied fields will be skipped. Once users rectify any needed info, they will land on the offer only screen.

Checkout

For client-side only apps

The embed widget's default mode includes a full checkout. Customers can go through the entire flow through to a secure purchase and policies will be delivered directly to their inbox.

To explicitly set the widget to include checkout, set the includeCheckout property to true.

For full stack apps

In server side and full-stack applications where customers are already purchasing other products, it's best to exclude the secure checkout built into the widget so customers can purchase their insurance alongside any other products as part of a single transaction. A backend integration (See Buddy's Partner API docs) is required in order to complete the transaction.

To use this mode, set includeCheckout to false, or set viewType to offer-only.

When users opt in, the onOptIn callback is fired with the completed application object. If a user opts out, onOptOut will be called.

// simple paginated example to collect data
import React from 'react';
import BuddyOfferElement from '@buddy-technology/offer-component';
import { handleAddToCart, handleRemoveFromCart } from './myApi';

function App() {
  const handleAddToCart = (payload) => {
    handleAddToCart(payload)
  };

  const handleRemoveFromCart = (payload) => {
    handleRemoveFromCart(payload)
  };

  return (
    <div id="app">
      <h1>My App</h1>
      <BuddyOfferElement
        ion="MY_ION"
        partnerID="my-partner-id"
        viewType="paginated"
        includeCheckout={false}
        onOptIn={handleAddToCart}
        onOptOut={handleRemoveFromCart}
        stage="PRODUCTION"
      />
    </div>
  );
}
export default App;
// example using offer-only and passing in customer/policy data
import React from 'react';
import BuddyOfferElement from '@buddy-technology/offer-component';
import { handleAddToCart, handleRemoveFromCart } from './myApi';

// passing down data as prop in this example
function App({ userAndPolicyData }) {
  const handleAddToCart = (payload) => {
    handleAddToCart(payload)
  };

  const handleRemoveFromCart = (payload) => {
    handleRemoveFromCart(payload)
  };

  return (
    <div id="app">
      <h1>My App</h1>
      <BuddyOfferElement
        ion="MY_ION"
        partnerID="my-partner-id"
        viewType="offer-only"
        data={userAndPolicyData} // data is passed in, if any fields are invalid, they will be displayed to user to rectify before purchasing.
        onOptIn={handleAddToCart}
        onOptOut={handleRemoveFromCart}
        stage="PRODUCTION"
      />
    </div>
  );
}
export default App;

Pre-filling Data

It's a best practice to pass in any customer and policy data that has already been collected to optimize user experience. Unless viewType is offer-only, users will be able to see and edit any data that is passed.

Data follows the application object structure. Refer to your specific ION (or offering) documentation.

import React from 'react';
import BuddyOfferElement from '@buddy-technology/offer-component';

function App() {
  const data = {
    customer: {
      firstName: 'customer first name',
      lastName: 'customer last name',
      address: {
        line1: 'street address',
        city: 'city',
        state: 'state',
        zip: 'zip',
      }
    },
    policy: {
      startDate: '12/31/2022',
    }
  };

  return (
    <div id="app">
      <h1>My App</h1>
      <BuddyOfferElement
        ion="MY_ION"
        partnerID="my-partner-id"
        data={data}
        stage="PRODUCTION"
      />
    </div>
  );
}
export default App;

Styling

Buddy's offer element supports extensive visual customization, allowing you to seamlessly match the design of your site with the theme prop.

  1. Pick a base theme and color palette: Quickly get running by picking the prebuilt theme and palette that most closely resembles your website.

  2. Customize even further by adding overrides: Virtually any property can be further customized using CSS object styling.

import React from 'react';
import BuddyOfferElement from '@buddy-technology/offer-component';

function App() {
  const customTheme = {
    baseTheme: "base",
    palette: "buddy",
    overrides: {
      webFonts: [
        'https://fonts.googleapis.com/css2?family=Rubik:wght@700&display=swap',
      ],
      styles: {
        h1: {
          color: 'var(--color-text-primary)',
          fontFamily: 'Rubik, sans-serif',
          fontSize: '2rem',
          '@media (min-width: 992px)': {
            fontSize: '3rem',
          },
          '&:hover': {
            boxShadow: 'none',
            backgroundColor: '#FBF9EF',
          },
        },
        body: { color: '#0A242D' },
        '.input-text': {
          border: 'none',
          color: '#333333',
        }
      },
      colors: { textPrimary: '#0A242D' },
    },
  };

  return (
    <div id="app">
      <h1>My App</h1>
      <BuddyOfferElement
        ion="MY_ION"
        partnerID="my-partner-id"
        theme={customTheme}
        stage="PRODUCTION"
      />
    </div>
  );
}
export default App;

Color Palettes

We currently offer two color palettes to start with (with more coming soon!).

Base:

primary: '#000000',
secondary: '#254E70',
tertiary: '#848484',
negative: '#e45b78',
positive: '#A3D9B1',
textPrimary: '#000',
textSecondary: '848484',
backgroundPrimary: '#fff',
backgroundSecondary: '#7d8387',

Buddy:

primary: '#DC5648', secondary: '#0A242D99', tertiary: '#46460E33', negative: '#D62719', positive: '#A3D9B1', textPrimary: '#0A242D', textSecondary: '#0A242D99', backgroundPrimary: 'linear-gradient(#D6F1F2, #F8F6D2)', backgroundSecondary: '#FBF9EF',

Overrides

The overrides object consists of three major properties:

webFonts: An array of string urls for web fonts. Note: if using web fonts, it is still necessary to set the fontFamily property on any elements using that particular webfont.

styles: A CSS styles object with any CSS overrides on a given theme. Styles will overwrite existing theme styles. Media queries and child selectors can be accessed as a string key name, and nested within a given selector (see the example above). Component classes can also be manipulated within the styles object.

colors: A colors object will override any of the color properties in the table below:

KeyWhere its used
primarygraphics, call-outs, primary buttons
secondaryfocused borders, accents
tertiaryinactive accents
negativeerror messages (note: this is currently set to "accessible red." be mindful of accessibility when overriding this property)
positivesuccess messages
textPrimaryprimary body copy
textSecondarysecondary copy, accents
backgroundPrimarymain background (containers)
backgroundSecondarysecondary backgrounds

Classes and Components

Some components can be directly modified by accessing their class names in the theme object. See the full list of component class names below.

  var customTheme = {
    style: {
      h1: { fontSize: 36 },
      '.button-primary': {
        // custom styles
      },
    },
  };

Available Classes

  • card
  • button-primary
  • button-secondary
  • input-text
  • input-invalid
  • input-select
  • input-label
  • input-label-invalid
  • offer-container
  • field-container
  • view-container

Capturing data

You can hook any CRM or analytics platform into the Buddy offer-element using the onUserEvent hook.

onUserEvent(eventType: string, data: object) ⇒ void

The onUserEvent hook property allows you to pass in a custom function to embed your analytics of choice. Simply pass in a callback to the onUseEvent option, and you will get the following 2 parameters:

  • eventType: string denoting the type of event that fired (ex: "onViewChange")
  • data: object with internal data regarding the event. All data objects include a timestamp property.

Quick example

import React from 'react';
import BuddyOfferElement from '@buddy-technology/offer-component';

function App() {
  const handleUserEvent = (payload) => {
    // logs an object with eventType and data to the console
    console.log(payload)
  };

  return (
    <div id="app">
      <h1>My App</h1>
      <BuddyOfferElement
        ion="MY_ION"
        partnerID="my-partner-id"
        viewType="offer-only"
        onUserEvent={handleUserEvent}
        stage="PRODUCTION"
      />
    </div>
  );
}
export default App;

Event types and data fired:

We capture several user-driven events and each has different available pieces of data associated with it. Look closely at the list below to determine which events you want to capture and how to use the data.

onOfferElementLoad

Triggers when the offer element mounts

data returned

NameTypeDescription
timestampnumberthe timestamp when the event occurred in milliseconds
partnerIdstringthe partner identifier
incomingDataFieldIdsstring[]an array of the field ids included in the incoming data

onOfferElementUpdate

Triggers when the data prop to the Offer Element is updated

data returned

NameTypeDescription
timestampnumberthe timestamp when the event occurred in milliseconds
partnerIdstringthe partner identifier
updatedDataFieldIdsstring[]an array of the field ids included in the updated data

onViewChange

Triggers when users click to a new screen in a paginated view mode.

data returned

NameTypeDescription
viewIdstringthe id of the current view
timestampnumberthe timestamp when the event occurred in milliseconds

onScrollToView

Triggers when or scroll to a section in single-page view mode.

data returned

NameTypeDescription
viewIdstringthe id of the current view
timestampnumberthe timestamp when the event occurred in milliseconds

onQuote

Triggers when the app displays retrieves a quote for the policy

data returned

NameTypeDescription
pricingnumberif successful, the price of the policy
errorstringif unsuccessful, a message explaining the type of error encountered when attempting to quote
timestampnumberthe timestamp when the event occurred in milliseconds

onCheckout

Triggers during the check out process

data returned

NameTypeDescription
checkoutStatusenumone of ['start', 'success', 'error']
premiumnumberthe total premium of the purchase
errorstringif unsuccessful, a message explaining the type of error encountered
timestampnumberthe timestamp when the event occurred in milliseconds

onFocus

Triggers when an input is focused

data returned

NameTypeDescription
elementIdstringthe id of the invalid field/input
timestampnumberthe timestamp when the event occurred in milliseconds

onBlur

Triggers when an input is blurred

data returned

NameTypeDescription
elementIdstringthe id of the invalid field/input
timestampnumberthe timestamp when the event occurred in milliseconds

onRadioSelection

Triggers when a radio button is selected

data returned

NameTypeDescription
elementIdstringthe id of the invalid field/input
valueanythe value of the selected radio
timestampnumberthe timestamp when the event occurred in milliseconds

onSlide

Triggers when a slider input is changed

data returned

NameTypeDescription
elementIdstringthe id of the invalid field/input
valueanythe value of the selected radio
timestampnumberthe timestamp when the event occurred in milliseconds

onCheckboxSelection

Triggers when a checkbox is selected

data returned

NameTypeDescription
elementIdstringthe id of the invalid field/input
checkedbooleanwhether the box is checked/unchecked
valuestringthe label of the selected checkbox
timestampnumberthe timestamp when the event occurred in milliseconds

onBlur

Triggers when an input is blurred

data returned

NameTypeDescription
elementIdstringthe id of the invalid field/input
timestampnumberthe timestamp when the event occurred in milliseconds

onValidationError

Triggers when an input is triggered as invalid

data returned

NameTypeDescription
elementIdstringthe id of the invalid field/input
validationErrorstringthe type of validation error encountered
timestampnumberthe timestamp when the event occurred in milliseconds

Triggers when an input is triggered as invalid

data returned

NameTypeDescription
externalLinkUrlstringthe url of the link
timestampnumberthe timestamp when the event occurred in milliseconds

onOptIn

Triggers when a user opts into an 'offer-only' insurance offer

data returned

NameTypeDescription
viewIdstringthe id of the view: 'offer-only'
premiumnumberthe total premium of the purchase
partnerIdstringthe partner id used for the offer
timestampnumberthe timestamp when the event occurred in milliseconds

Google Analytics example

import React from 'react';
import BuddyOfferElement from '@buddy-technology/offer-component';

function App() {
  /* This is an example of how you could use these events
  your usage may vary depending on your analytics systems */

  const handleUserEvent = (e) => {
    // this block shows a condition looking for checkout and success, 
    // this is a completed purchase using the GA 'purchase' event for conversion
    if (e.eventType == "onCheckout" && e.data.checkoutStatus == "success") {
      gtag("event", "purchase", {
        transaction_id: e.data.orderID,
        value: e.data.premium,
        items: [{
          item_id: "SKU_12345",
          item_name: '${ion}',
          currency: "USD",
          price: e.data.premium,
        }]
      });
    }

    // This block tracks which views are displayed 
    // this is configured to use the built-in GA event 'screen_view'
    if (e.eventType == "onViewChange") {
      gtag('event', 'screen_view', {
        'app_name': 'in-offer-element',
        'screen_name': e.data.viewID
      });
    }
    // This block tracks when quotes are made
    // This is configured to use a custom event and captures a custom dimension
    if (e.eventType == "onQuote") {
      gtag('event', 'quote', {
        'app_name': 'in-offer-element',
        'price': e.data.pricing,
      });
    }
  }
  };

  return (
    <div id="app">
      <h1>My App</h1>
      <BuddyOfferElement
        ion="MY_ION"
        partnerID="my-partner-id"
        viewType="offer-only"
        onUserEvent={handleUserEvent}
        stage="PRODUCTION"
      />
    </div>
  );
}
export default App;

版本与发布记录

@buddy-technology/offer-component 共发布 14 个版本,首次发布于 2022/09/09;过去 12 个月发布了 3 个版本。

版本发布日期距今
1.7.1latest2026/03/267 个月前
1.7.02026/02/049 个月前
1.6.02026/01/299 个月前
1.5.02025/03/211.6 年前
1.4.02024/08/302 年前
1.3.02024/06/102 年前

关于 @buddy-technology/offer-component 的常见问题

@buddy-technology/offer-component 是什么?

--- React component for adding Buddy's Insurance Gateway

如何安装 @buddy-technology/offer-component?

在项目目录运行 npm install @buddy-technology/offer-component,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(^16.8.0 || ^17.0.0 || ^18.0.0 || ^19.0.0)、react-dom(^16.8.0 || ^17.0.0 || ^18.0.0 || ^19.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@buddy-technology/offer-component 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@buddy-technology/offer-component 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@buddy-technology/offer-component 可以商用吗?

@buddy-technology/offer-component 使用 UNLICENSED 许可。作者声明为 UNLICENSED,即不授予公开使用许可(与公共领域的 Unlicense 不同)。使用前需获得作者授权。以上为概括,不构成法律意见,请以许可原文为准。

@buddy-technology/offer-component 的下载量有多少?

根据 npm 官方下载统计,@buddy-technology/offer-component 在 2026/10/09 – 2026/11/07 这 30 天内被下载 2,016 次,日均约 67 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@buddy-technology/offer-component 还在维护吗?

最新版本 1.7.1 发布于 2026/03/26(7 个月前);至今共发布 14 个版本,其中过去 12 个月发布了 3 个;首次发布于 2022/09/09。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。