Usage
Configuration
All of the embedded offer framework packages consume a standard config object for setting up the offer.
export interface Config {
/**
* Client ID part of your API key.
*/
client_id: string;
/**
* Theme options
*/
theme?: Theme;
/**
* A unique id for this offer. This value should be defined if there are
* multiple offers being presented on a single page. You can typically
* set this value to an order id, or an item id.
*/
unique_offer_id?: string;
/**
* Payment configuration
*/
payments?: PaymentElementConfig;
/**
* Customer details. If this customer is provided, it will override any customer
* defined in any specific product configuration.
*/
customer?: Customer;
/**
* Configuration / quote information for various products
*/
product_config?: ProductConfig;
}
- JavaScript
- React
- Vue
You'll notice the reference for #offer
when instantiating the VerticalInsure
object below. This is
a document query selector and will be the element which we will mount the embedded offer.
<!-- Element where the offer will be mounted -->
<div id="offer"></div>
<script>
new VerticalInsure("#offer", {
client_id: "test_********************************",
product_config: {
"gap-medical": [{
"customer": {
"email_address": "user@example.com",
"first_name": "James",
"last_name": "Doe",
"state": "MN",
"postal_code": "55432"
},
"policy_attributes": {
"coverage_end_date": "YYYY-MM-DD",
"coverage_start_date": "YYYY-MM-DD",
"coverage_type": "SOCCER",
"covered_person": {
"birth_date": "YYYY-MM-DD",
"state": "MN",
"first_name": "John",
"last_name": "Doe",
"street": "NA"
}
}
}]
}
});
</script>
import { VerticalInsure } from '@vertical-insure/embedded-offer-react';
export default function App() {
const ref = useRef(null);
return (
<VerticalInsure
ref={ref}
config={{
client_id: "test_********************************",
product_config: {
"gap-medical": [{
"customer": {
"email_address": "user@example.com",
"first_name": "James",
"last_name": "Doe",
"state": "MN",
"postal_code": "55432"
},
"policy_attributes": {
"coverage_end_date": "YYYY-MM-DD",
"coverage_start_date": "YYYY-MM-DD",
"coverage_type": "SOCCER",
"covered_person": {
"birth_date": "YYYY-MM-DD",
"state": "MN",
"first_name": "John",
"last_name": "Doe",
"street": "NA"
}
}
}]
}
}}
/>
)
}
<script lang="ts" setup>
import { ref } from 'vue'
import { VerticalInsure } from "@vertical-insure/embedded-offer-vue";
const config = ref({
client_id: "test_********************************",
product_config: {
"gap-medical": [{
"customer": {
"email_address": "user@example.com",
"first_name": "John",
"last_name": "Doe",
"state": "MN",
"postal_code": "55414"
},
"policy_attributes": {
"coverage_end_date": "2025-06-25",
"coverage_start_date": "2025-01-25",
"coverage_type": "SOCCER",
"covered_person": {
"birth_date": "2010-07-05",
"state": "MN",
"first_name": "John",
"last_name": "Doe",
"street": "NA"
}
}
}]
}
})
</script>
<template>
<div>
<VerticalInsure :config="config" />
</div>
</template>
Listening for Events
The embedded offer packages fire a standard JavaScript CustomEvent named offer-state-change
.
You can listen to this event using the standard addEventListener approach.
window.addEventListener("offer-state-change", (e) => {
console.log(JSON.stringify(e.detail.quotes, null, 2));
});
This event is fired upon user interaction with the embedded offer and returns the accepted quotes that the user has selected. This means the event could come back with an empty array if no quote has been selected.
Our various packages also provide a standard way to subscribe to this event which provides an abstraction on top of the basic addEventListener
functionality.
- JavaScript
- React
- Vue
<!-- Element where the offer will be mounted -->
<div id="offer"></div>
<script>
new VerticalInsure("#offer", {
client_id: "test_********************************",
product_config: {
"gap-medical": [{
"customer": {
"email_address": "user@example.com",
"first_name": "James",
"last_name": "Doe",
"state": "MN",
"postal_code": "55432"
},
"policy_attributes": {
"coverage_end_date": "YYYY-MM-DD",
"coverage_start_date": "YYYY-MM-DD",
"coverage_type": "SOCCER",
"covered_person": {
"birth_date": "YYYY-MM-DD",
"state": "MN",
"first_name": "John",
"last_name": "Doe",
"street": "NA"
}
}
}]
}
},
function(offerState) {
console.log(JSON.stringify(offerState.quotes));
});
</script>
import { VerticalInsure } from '@vertical-insure/embedded-offer-react';
export default function App() {
const ref = useRef(null);
return (
<VerticalInsure
ref={ref}
onOfferStateChange={(offerState) => {
console.log(JSON.stringify(offerState.quotes));
}}
/>
)
}
<script lang="ts" setup>
import { ref } from 'vue'
import { VerticalInsure } from "@vertical-insure/embedded-offer-vue";
const config = ref({
client_id: "test_********************************",
product_config: {
"gap-medical": [{
"customer": {
"email_address": "user@example.com",
"first_name": "John",
"last_name": "Doe",
"state": "MN",
"postal_code": "55414"
},
"policy_attributes": {
"coverage_end_date": "2025-06-25",
"coverage_start_date": "2025-01-25",
"coverage_type": "SOCCER",
"covered_person": {
"birth_date": "2010-07-05",
"state": "MN",
"first_name": "John",
"last_name": "Doe",
"street": "NA"
}
}
}]
}
})
function offerStateChangeHandler(offerState) {
console.log(offerState);
}
</script>
<template>
<div>
<VerticalInsure :config="config" @offer-state-change="offerStateChangeHandler"/>
</div>
</template>
Actions
The embedded offer supports 3 different methods that can be used to interact with the offer in various ways.
- validate: Validates the form in the offer to determine if the user has completed selecting required choices.
- tokenizePaymentMethod: If payments are configured on the offer, tokenizes the payment method into a PCI-compliant token for transport to your backend.
- update: Updates the offer with new data for the quote.
With React and Vue, you don't need to explicitly perform update commands, as updating the input config update will sync these changes automatically.
- JavaScript
- React
- Vue
<div id="offer"></div>
<button id="validate-button">Validate</button>
<button id="tokenize-button">Tokenize Payment Method</button>
<button id="update-button">Update Quote</button>
<script>
const verticalInsure = new VerticalInsure("#offer", {
client_id: "test_********************************",
product_config: {
"gap-medical": [{
"customer": {
"email_address": "user@example.com",
"first_name": "James",
"last_name": "Doe",
"state": "MN",
"postal_code": "55432"
},
"policy_attributes": {
"coverage_end_date": "YYYY-MM-DD",
"coverage_start_date": "YYYY-MM-DD",
"coverage_type": "SOCCER",
"covered_person": {
"birth_date": "YYYY-MM-DD",
"state": "MN",
"first_name": "John",
"last_name": "Doe",
"street": "NA"
}
}
}]
}
},
function(offerState) {
console.log(JSON.stringify(offerState.quotes));
});
const validateButton = document.getElementById("validate-button");
validateButton.addEventListener("click", async (e) => {
e.preventDefault();
const status = await verticalInsure.validate();
console.log(status);
});
const tokenizeButton = document.getElementById("tokenize-button");
tokenizeButton.addEventListener("click", async (e) => {
e.preventDefault();
const token = await verticalInsure.tokenizePaymentMethod();
console.log(token);
});
const updateButton = document.getElementById("update-button");
updateButton.addEventListener("click",(e) => {
e.preventDefault();
verticalInsure.update({
"gap-medical": [{
"customer": {
"email_address": "user@example.com",
"first_name": "James",
"last_name": "Doe",
"state": "MN",
"postal_code": "55432"
},
"policy_attributes": {
"coverage_end_date": "YYYY-MM-DD",
"coverage_start_date": "YYYY-MM-DD",
"coverage_type": "SOCCER",
"covered_person": {
"birth_date": "YYYY-MM-DD",
"state": "MN",
"first_name": "John",
"last_name": "Doe",
"street": "NA"
}
}
}]
});
});
</script>
import { VerticalInsure } from '@vertical-insure/embedded-offer-react';
export default function App() {
const ref = useRef(null);
return (
<>
<VerticalInsure
ref={ref}
onOfferStateChange={(offerState) => {
console.log(JSON.stringify(offerState.quotes));
}}
/>
<button onClick={() => ref.current?.validate()}>Validate</button>
<button onClick={() => ref.current?.tokenizePaymentMethod()}>Tokenize Payment Method</button>
</>
)
}
<script lang="ts" setup>
import { ref, useTemplateRef } from 'vue'
import VerticalInsure from "./components/VerticalInsure.vue";
import { OfferStateChangeEvent } from '@vertical-insure/embedded-offer';
const config = ref({
client_id: "test_RGMDV4FV4BNK4TSPT7DOQVC3P9HKEXTQ",
product_config: {
"gap-medical": [{
"customer": {
"email_address": "user@example.com",
"first_name": "John",
"last_name": "Doe",
"state": "MN" as const,
"postal_code": "55414"
},
"policy_attributes": {
"coverage_end_date": "2025-06-25",
"coverage_start_date": "2025-02-25",
"coverage_type": "SOCCER",
"covered_person": {
"birth_date": "2010-07-05",
"state": "MN",
"first_name": "John",
"last_name": "Doe",
"street": "NA"
}
}
}]
}
});
const viRef = useTemplateRef("verticalInsure");
function validate() {
viRef.value?.verticalInsure?.validate();
}
function tokenize() {
viRef.value?.verticalInsure?.tokenizePaymentMethod();
}
function changeHandler(offerState: OfferStateChangeEvent) {
console.log(offerState);
}
</script>
<template>
<div>
<VerticalInsure :config="config" ref="verticalInsure" @change="changeHandler"/>
<button @click="validate">Validate</button>
<button @click="tokenize">Tokenize</button>
</div>
</template>
Theming
The standard config object accepts the following theme input
export interface Theme {
colors?: {
background?: string;
primary?: string;
primary_contrast?: string;
secondary?: string;
secondary_contrast?: string;
neutral?: string;
neutral_contrast?: string;
error?: string;
error_contrast?: string;
success?: string;
success_contrast?: string;
border?: string;
},
font_family?: string;
components?: {
border_radius?: string;
}
}
Fonts
The font_family
field can accept any Google font name. For example, the following config will use Roboto
as the standard font in the offer.
{
theme: {
font_family: 'Roboto'
}
}