Skip to content

Commit

Permalink
add formId to the event object
Browse files Browse the repository at this point in the history
  • Loading branch information
zizzfizzix committed Jul 22, 2023
1 parent 724924a commit 924c24d
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ The structure of the object pushed to the dataLayer is the following:
```typescript
{
event: "contact_form_submit",
formId: string,
formData: {
name: string,
email: string,
Expand Down
2 changes: 2 additions & 0 deletions js/src/lib/findFormId.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const findFormId = (objKey: string) =>
/.+_([^_]+)/.exec(objKey)?.[1] ?? "0";
2 changes: 2 additions & 0 deletions js/src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { findKeyInObject } from "./findKeyInObject";
export { findFormId } from "./findFormId";
3 changes: 2 additions & 1 deletion js/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Based on https://gist.github.com/Maximoo/e42c0ac114d12873ab511b7a097e669e
import { findKeyInObject } from "./lib/findKeyInObject";
import { findKeyInObject, findFormId } from "./lib";

// TODO: get this from plugin settings with a default
const dataLayerVar = "dataLayer";
Expand All @@ -22,6 +22,7 @@ jQuery(document).on("ajaxSuccess", (_event, xhr, req, data) => {

window[dataLayerVar].push({
event: "contact_form_submit",
formId: findFormId(findKeyInObject(reqData, "et_pb_contactform")),
formData: {
name: reqData[findKeyInObject(reqData, "et_pb_contact_name")],
email: reqData[findKeyInObject(reqData, "et_pb_contact_email")],
Expand Down

0 comments on commit 924c24d

Please sign in to comment.