-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6bd69aa
commit 57e78f5
Showing
5 changed files
with
78 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}, | ||
(err) => { | ||
console.error('Error: ', err); | ||
}, | ||
); | ||
|
||
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters