@hyperse-hub/hps-plugin-campaign
Vendure plugin for marketing campaigns: translatable campaign content, optional Vendure promotions, coupon claim flows (with reCAPTCHA), campaign bundles, and storefront helpers on Order, Customer, and Collection.
Requirements
- Vendure
>= 3.6 (plugin compatibility >= 3.0.0)
- Node
>= 16
- Companion Hyperse plugins used at runtime:
@hyperse-hub/hps-plugin-core
@hyperse-hub/hps-plugin-email (email templates and job pipeline)
- The shop API references
RecaptchaInput for claim mutations; your combined Shop API schema must define it (for example via another Hyperse/Vendure extension that supplies reCAPTCHA).
Installation
Add the package to your Vendure server project and install peer dependencies (@vendure/core, Nest, GraphQL, TypeORM, etc.) as usual for your stack.
Registering the plugin
Import HpsPluginCampaign and register it together with HpsPluginCore and HpsPluginEmail (the campaign plugin declares these as imports).
Minimal example:
import { HpsPluginCore } from '@hyperse-hub/hps-plugin-core';
import { HpsPluginEmail } from '@hyperse-hub/hps-plugin-email';
import { HpsPluginCampaign } from '@hyperse-hub/hps-plugin-campaign';
export const config = {
plugins: [
HpsPluginCore.init({}),
HpsPluginEmail.init({}),
HpsPluginCampaign.init({
}),
],
};
Local dev server reference: config/dev-server.ts in this package.
Configuration
Pass options to HpsPluginCampaign.init({ ... }):
| Option | Type | Description |
|---|
initialCampaigns | CreateCampaignInput[] or (ctx: { channelId: string }) => CreateCampaignInput[] | Promise<...> | If set, campaigns are upserted on bootstrap (server process), scoped per channel. |
facetCampaignConfigFields | (defaultFields: StructFieldConfig[]) => StructFieldConfig[] | Extend the struct stored on FacetValue.campaignConfigData. Default includes an endsAt datetime field; override to add shop-specific fields (e.g. lens promos). |
expiredCouponRetentionDays | number | Days after coupon endsAt before the scheduled cleanup deletes rows. Default: 90. |
Types are declared in HpsPluginCampaignOptions (src/types.ts).
Concepts
Campaign types
CampaignType describes the business pattern (for example direct discount vs. new-user coupon). Values include:
DirectDiscount
NewUserFixedAmount
AllUserFixedAmount
BuyXGetYFreeWithLensDiscount
NewUserFirstPair
The enum is part of the plugin GraphQL schema and re-exported from the package entry for TypeScript consumers.
Facet for catalog tagging
The plugin defines a facet code constant: CAMPAIGN_FACET_CODE = 'campaign' (see src/constants.ts). Use this facet to tag product variants or attributes that belong to campaign merchandising; optional FacetValue struct field campaignConfigData can hold per-value campaign metadata (end dates, custom promo data).
- A campaign may reference a Vendure
promotionId.
- When
requiresCouponClaim is true, shoppers obtain CampaignCoupon rows via shop mutations; couponValidityDays controls lifetime after claim.
- Bundles group multiple campaigns and support a single “claim all” flow (
claimCampaignBundleCoupons).
Custom fields
Registered on bootstrap:
| Entity | Field | Purpose |
|---|
| Collection | campaign | Optional relation to Campaign for themed collection / PLP hero content. Public for shop resolution. |
| FacetValue | campaignConfigData | Optional struct for direct-discount / per-option promo display; shape extended via facetCampaignConfigFields. |
GraphQL API (overview)
Admin API
- Queries:
campaign, campaigns, campaignByCode
- Mutations:
createCampaign, updateCampaign, disableCampaign, deleteCampaign
- Additional admin operations exist for campaign coupons and campaign bundles (list/create/update/delete as defined in the bundled schema).
Inputs include localized CampaignTranslationInput with CampaignClaimFormContentInput and CampaignClaimResultContentInput for landing / post-claim UI copy and assets.
Shop API
- Queries:
campaignByCode, availableCampaigns, campaignBundleByCode
- Mutations:
claimCampaignCoupon, claimCampaignBundleCoupons — both take code, emailAddress, and recaptcha: RecaptchaInput!
- Extended types:
Order.availableCoupons
Customer.availableCoupons
Collection.campaign
availableCampaigns returns storefront-visible campaigns (enabled + within optional date window). It optionally accepts campaignTypes: [CampaignType!] so clients can group campaigns by campaignType when building coupons pages.
Claim results use CampaignCouponClaimStatus: CREATED, REFRESHED, or REJECTED, with optional message.
Scheduled task
The plugin registers a daily task (midnight cron) clean-expired-campaign-coupons-task that removes expired coupon rows according to expiredCouponRetentionDays.
Email
On bootstrap the plugin registers an external email handler campaign-coupon-claimed with HpsPluginEmail, using templates from the built templates/dist output. Ensure your email plugin is configured (transport, from address, etc.) so claim confirmation emails can be queued.
Events
CampaignCouponEvent is emitted when campaign coupons are created, updated, or deleted (src/events/campaign-coupon-event.ts). Subscribe in your Vendure EventBus for integrations or auditing.
Package exports
From @hyperse-hub/hps-plugin-campaign:
HpsPluginCampaign plugin class
CampaignType enum type
- Entities and events (e.g.
CampaignCouponEvent)
- Services:
CampaignService, CampaignCouponService, CampaignBundleService, CampaignProductVariantService
License
MIT — see LICENSE.