ej_beehiiv

Segment <> beehiiv Function

Sep 6th, 2022
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Learn more about destination functions API at
  2. // https://segment.com/docs/connections/destinations/destination-functions
  3.  
  4. /**
  5.  * Handle track event
  6.  * @param  {SegmentTrackEvent} event
  7.  * @param  {FunctionSettings} settings
  8.  */
  9. async function onTrack(event, settings) {
  10.     // Learn more at https://segment.com/docs/connections/spec/track/
  11.     const endpoint = 'https://api.beehiiv.com/v1/subscribers'; // replace with your endpoint
  12.     const apiKey = settings.apiKey;
  13.     const publicationId = settings.publicationId;
  14.     const sendWelcomeEmail = settings.sendWelcomeEmail;
  15.     let response;
  16.  
  17.     try {
  18.         response = await fetch(endpoint, {
  19.             method: 'POST',
  20.             headers: {
  21.                 'X-ApiKey': apiKey,
  22.                 'Content-Type': 'application/json'
  23.             },
  24.             body: JSON.stringify({
  25.                 email: event.properties.email,
  26.                 publication_id: publicationId,
  27.                 send_welcome_email: sendWelcomeEmail,
  28.                 utm_source: event.properties.utm_source
  29.             })
  30.         });
  31.     } catch (error) {
  32.         // Retry on connection error
  33.         throw new RetryError(error.message);
  34.     }
  35.  
  36.     if (response.status >= 500 || response.status === 429) {
  37.         // Retry on 5xx (server errors) and 429s (rate limits)
  38.         throw new RetryError(`Failed with ${response.status}`);
  39.     }
  40. }
  41.  
  42. /**
  43.  * Handle identify event
  44.  * @param  {SegmentIdentifyEvent} event
  45.  * @param  {FunctionSettings} settings
  46.  */
  47. async function onIdentify(event, settings) {
  48.     // Learn more at https://segment.com/docs/connections/spec/identify/
  49.     throw new EventNotSupported('identify is not supported');
  50. }
  51.  
  52. /**
  53.  * Handle group event
  54.  * @param  {SegmentGroupEvent} event
  55.  * @param  {FunctionSettings} settings
  56.  */
  57. async function onGroup(event, settings) {
  58.     // Learn more at https://segment.com/docs/connections/spec/group/
  59.     throw new EventNotSupported('group is not supported');
  60. }
  61.  
  62. /**
  63.  * Handle page event
  64.  * @param  {SegmentPageEvent} event
  65.  * @param  {FunctionSettings} settings
  66.  */
  67. async function onPage(event, settings) {
  68.     // Learn more at https://segment.com/docs/connections/spec/page/
  69.     throw new EventNotSupported('page is not supported');
  70. }
  71.  
  72. /**
  73.  * Handle screen event
  74.  * @param  {SegmentScreenEvent} event
  75.  * @param  {FunctionSettings} settings
  76.  */
  77. async function onScreen(event, settings) {
  78.     // Learn more at https://segment.com/docs/connections/spec/screen/
  79.     throw new EventNotSupported('screen is not supported');
  80. }
  81.  
  82. /**
  83.  * Handle alias event
  84.  * @param  {SegmentAliasEvent} event
  85.  * @param  {FunctionSettings} settings
  86.  */
  87. async function onAlias(event, settings) {
  88.     // Learn more at https://segment.com/docs/connections/spec/alias/
  89.     throw new EventNotSupported('alias is not supported');
  90. }
  91.  
  92. /**
  93.  * Handle delete event
  94.  * @param  {SegmentDeleteEvent} event
  95.  * @param  {FunctionSettings} settings
  96.  */
  97. async function onDelete(event, settings) {
  98.     // Learn more at https://segment.com/docs/partners/spec/#delete
  99.     throw new EventNotSupported('delete is not supported');
  100. }
Advertisement
Add Comment
Please, Sign In to add comment