Skip to content

Commit

Permalink
Merge pull request #669 from Kit/emit-js-custom-event-on-subscribe
Browse files Browse the repository at this point in the history
Emit JS `CustomEvent` when a user subscribes to a ConvertKit Form
  • Loading branch information
n7studios authored Nov 25, 2024
2 parents cb4e040 + c1155bf commit 2730e8f
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions resources/frontend/js/convertkit.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,24 @@ function convertStoreSubscriberEmailAsIDInCookie( emailAddress ) {
if ( convertkit.debug ) {
console.log( response );
}

return response.json();
}
)
.then(
function ( result ) {
if ( convertkit.debug ) {
console.log( result );
}

// Emit custom event with subscriber ID.
convertKitEmitCustomEvent(
'convertkit_user_subscribed',
{
id: result.data.id,
email: emailAddress
}
);
}
)
.catch(
Expand Down Expand Up @@ -181,6 +192,24 @@ function convertKitSleep( milliseconds ) {

}

/**
* Emit a custom event with optional detail data.
*
* This function creates and dispatches a custom event with the specified
* event name and detail data.
*
* @since 2.5.0
*
* @param {string} eventName The name of the custom event to emit.
* @param {Object} [detail={}] Optional detail data to include with the event.
*/
function convertKitEmitCustomEvent( eventName, detail ) {

const event = new CustomEvent( eventName, { detail } );
document.dispatchEvent( event );

}

/**
* Register events
*/
Expand Down

0 comments on commit 2730e8f

Please sign in to comment.