Skip to content

Commit

Permalink
basic events
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderkirtzel committed Sep 27, 2024
1 parent 6bd69aa commit 57e78f5
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 1 deletion.
8 changes: 8 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"@elbwalker/jest": "*",
"@elbwalker/tsconfig": "*",
"@elbwalker/tsup": "*",
"@types/facebook-nodejs-business-sdk": "^20.0.2",
"eslint-config-custom": "*",
"facebook-nodejs-business-sdk": "^20.0.3",
"turbo": "^2.0.5",
Expand Down
2 changes: 2 additions & 0 deletions packages/destinations/node/meta/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@
</p>

# Meta destination for walkerOS

[Meta Conversion API](https://developers.facebook.com/docs/marketing-api/conversions-api) (CAPI)
66 changes: 65 additions & 1 deletion packages/destinations/node/meta/src/push.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,72 @@
import type { Config, PushEvents } from './types';
import type { WalkerOS } from '@elbwalker/types';
import type { Config, Mapping, PushEvents } from './types';
import bizSdk from 'facebook-nodejs-business-sdk';

export const push = async function (events: PushEvents, config: Config) {
const { access_token, pixel_id } = config.custom;
events;
config;

bizSdk.FacebookAdsApi.init(access_token);

const serverEvents = events.map((event) =>
mapEvent(event.event, event.mapping),
);

const EventRequest = bizSdk.EventRequest;
const eventRequest = new EventRequest(access_token, pixel_id).setEvents(
serverEvents,
);

eventRequest.execute().then(
(response) => {
console.log('Response: ', response);

Check warning on line 23 in packages/destinations/node/meta/src/push.ts

View workflow job for this annotation

GitHub Actions / Build and test

Unexpected console statement
},
(err) => {
console.error('Error: ', err);

Check warning on line 26 in packages/destinations/node/meta/src/push.ts

View workflow job for this annotation

GitHub Actions / Build and test

Unexpected console statement
},
);

return { queue: [] };
};

export const mapEvent = (
event: WalkerOS.Event,
mapping: Mapping = {},
): bizSdk.ServerEvent => {
mapping; // @TODO

const Content = bizSdk.Content;
const CustomData = bizSdk.CustomData;
const UserData = bizSdk.UserData;
const ServerEvent = bizSdk.ServerEvent;

let userData = new UserData();
// @TODO
// .setEmails(['[email protected]'])
// .setPhones(['12345678901', '14251234567'])
// .setFbp('fb.1.1558571054389.1098115397') // _fbp cookie
// .setFbc('fb.1.1554763741205.AbCdEfGhIjKlMnOpQrStUvWxYz1234567890'); // Facebook Click ID

const { user, source } = event;

if (user.userAgent) userData = userData.setClientUserAgent(user.userAgent);
if (user.ip) userData = userData.setClientIpAddress(user.ip);

const content = new Content().setId('product123').setQuantity(1); // @TODO

const customData = new CustomData()
.setContents([content])
.setCurrency('usd') // @TODO
.setValue(123.45); // @TODO

const serverEvent = new ServerEvent()
.setEventName(event.event)
.setEventTime(event.timestamp)
.setUserData(userData)
.setCustomData(customData)
.setEventSourceUrl(source.id)
.setActionSource(source.type);

return serverEvent;
};
2 changes: 2 additions & 0 deletions packages/destinations/node/meta/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export type PartialConfig = NodeDestination.Config<
Partial<CustomEventConfig>
>;

export type Mapping = NodeDestination.EventConfig<CustomEventConfig>;

export type PushEvents = NodeDestination.PushEvents<CustomEventConfig>;

export interface CustomConfig {
Expand Down

0 comments on commit 57e78f5

Please sign in to comment.