Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: identify and track for loops #3976

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions src/cdk/v2/destinations/loops/procWorkflow.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
bindings:
- name: EventType
path: ../../../../constants
- path: ../../bindings/jsontemplate
exportAll: true
- name: removeUndefinedAndNullValues
path: ../../../../v0/util
- name: defaultRequestConfig
path: ../../../../v0/util

steps:
- name: messageType
template: |
.message.type.toLowerCase();

- name: validateInput
template: |
$.assert(.message.type, "message Type is not present. Aborting message.");
$.assert(.message.type in {{$.EventType.([.TRACK, .IDENTIFY])}},
"message type " + .message.type + " is not supported");

- name: validateIdentifyEmail
condition: $.outputs.messageType === {{$.EventType.IDENTIFY}}
template: |
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const email = .message.({{{{$.getGenericPaths("emailOnly")}}}});

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this line do?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will assign email value to available generic mapping of message.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you suggest using that for payload.userId when creating the payloads further down, rather than using .message.userId?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes.
Also for payload creation you can do something like this:

$.context.payload = .message.({
        event: .event,
        userId: {{{{$.getGenericPaths("userIdOnly")}}}},
        email: {{{{$.getGenericPaths("emailOnly")}}}},
      });

this means =>

$.context.payload = {
    event: "event_name",
    userId: "some_user_id",
    email: "[email protected]"
}

const email = .message.({{{{$.getGenericPaths("emailOnly")}}}});
$.assert(email, "email is required. Aborting");

- name: validateTrackIdentifier
condition: $.outputs.messageType === {{$.EventType.TRACK}}
template: |
const userId = .message.({{{{$.getGenericPaths("userIdOnly")}}}});
const email = .message.({{{{$.getGenericPaths("emailOnly")}}}});
$.assert(email || userId, "Either email or userId is required. Aborting");

- name: validateEventName
condition: $.outputs.messageType === {{$.EventType.TRACK}}
template: |
$.assert(.message.event, "event is required for track call")

- name: prepareContext
template: |
$.context.messageType = .message.type.toLowerCase();
$.context.payload = {};

- name: identifyPayload
condition: $.outputs.messageType === {{$.EventType.IDENTIFY}}
template: |
$.context.endpoint = "https://app.loops.so/api/v1/contacts/update";
const payload = {}
Object.assign(payload, .message.context.traits);
payload.userId = .message.({{{{$.getGenericPaths("userIdOnly")}}}});
payload.email = .message.({{{{$.getGenericPaths("emailOnly")}}}});
$.context.payload = payload;

- name: trackPayload
condition: $.outputs.messageType === {{$.EventType.TRACK}}
template: |
$.context.endpoint = "https://app.loops.so/api/v1/events/send";
const payload = {}
Object.assign(payload, .message.context.traits);
payload.userId = .message.({{{{$.getGenericPaths("userIdOnly")}}}});
payload.email = .message.({{{{$.getGenericPaths("emailOnly")}}}});
payload.eventName = .message.event;
payload.eventProperties = .message.properties;
$.context.payload = payload;
danrowden marked this conversation as resolved.
Show resolved Hide resolved

- name: buildResponse
template: |
const response = $.defaultRequestConfig();
response.body.JSON = $.context.payload;
response.endpoint = $.context.endpoint;
response.headers = {
"authorization": "Bearer " + .destination.Config.apiKey,
"content-type": "application/json"
};
response
Loading