Skip to content

Commit f15646d

Browse files
committed
Refactoring
1 parent c410a07 commit f15646d

File tree

13 files changed

+81
-94
lines changed

13 files changed

+81
-94
lines changed

README.md

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
Facebook Conversion API for Next.js
1+
Facebook / Meta Conversion API for Next.js
22

33
> Next.js wrapper for [Facebook's Conversion API](https://developers.facebook.com/docs/marketing-api/conversions-api/)
44
5-
# Facebook Conversion API for Next.js
5+
# Facebook / Meta Conversion API for Next.js
66
This package helps you implement Facebook Conversion API in Next.js.
77

88
It includes an API route handler for sending server-side events to Facebook and client-side functions to trigger the events.
@@ -66,22 +66,24 @@ Trigger the events you need. For example, add to cart or purchase events.
6666
```jsx
6767
import { fbEvent } from '@rivercode/facebook-conversion-api-nextjs';
6868

69-
fbEvent({
70-
eventName: 'ViewContent', // ViewContent, AddToCart, InitiateCheckout or Purchase
71-
eventId: 'eventId', // optional, unique event id's will be generated by default
72-
emails: ['email1', 'email2'], // optional
73-
phones: ['phone1', 'phone2'], // optional
74-
firstName: 'firstName', // optional
75-
lastName: 'lastName', // optional
76-
country: 'country', // optional
77-
city: 'city', // optional
78-
zipCode: 'zipCode', // optional
79-
products: [{ // optional
80-
sku: 'product123',
81-
quantity: 1,
82-
}],
83-
value: 1000, // optional
84-
currency: 'USD', // optional
85-
enableStandardPixel: false // default false (Require Facebook Pixel to be loaded, see step 2)
86-
});
69+
useEffect(() => {
70+
fbEvent({
71+
eventName: 'ViewContent', // ViewContent, AddToCart, InitiateCheckout, Purchase etc.
72+
eventId: 'eventId', // optional, unique event id's will be generated by default
73+
emails: ['email1', 'email2'], // optional
74+
phones: ['phone1', 'phone2'], // optional
75+
firstName: 'firstName', // optional
76+
lastName: 'lastName', // optional
77+
country: 'country', // optional
78+
city: 'city', // optional
79+
zipCode: 'zipCode', // optional
80+
products: [{ // optional
81+
sku: 'product123',
82+
quantity: 1,
83+
}],
84+
value: 1000, // optional
85+
currency: 'USD', // optional
86+
enableStandardPixel: false // default false (Require Facebook Pixel to be loaded, see step 2)
87+
});
88+
}, []);
8789
```
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import { Arguments } from './graph-api.types';
1+
type Arguments = {
2+
endpoint: string
3+
body: any
4+
};
25

36
/**
47
* Facebook Graph API client.

src/api/graph/graph-api.types.ts

Lines changed: 0 additions & 4 deletions
This file was deleted.

src/api/graph/index.ts

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/components/FBPixelProvider.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ const FBPixelProvider = ({ children }: Props) => {
1010
const router = useRouter();
1111

1212
useEffect(() => {
13-
if (!router.asPath.includes('?')) {
14-
fbPageView();
15-
}
13+
fbPageView();
1614

1715
router.events.on('routeChangeComplete', fbPageView);
1816
return () => {

src/conversion-api.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { v4 as uuidv4 } from 'uuid';
22
import debug from './utils/debug';
3-
import FBEventType from '../types';
3+
import { FBEvent } from './types';
44

55
declare global {
66
interface Window {
@@ -25,7 +25,7 @@ const fbPageView = (): void => {
2525
* @param event
2626
* @constructor
2727
*/
28-
const fbEvent = (event: FBEventType): void => {
28+
const fbEvent = (event: FBEvent): void => {
2929
const eventId = event.eventId ? event.eventId : uuidv4();
3030

3131
if (event.enableStandardPixel) {

src/handlers/event-handler.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,27 @@
11
import type { NextApiRequest, NextApiResponse } from 'next';
22
import { getClientIpAddress, getClientFbp, getClientFbc } from '../utils/request';
33
import { sendServerSideEvent } from '../services/server-side-events';
4-
import { Arguments } from './event-handler.types';
4+
5+
type Arguments = {
6+
eventName: string
7+
eventId: string
8+
emails?: Array<string> | null
9+
phones?: Array<string> | null
10+
firstName?: string
11+
lastName?: string
12+
country?: string
13+
city?: string
14+
zipCode?: string
15+
products: {
16+
sku: string
17+
quantity: number
18+
}[]
19+
value?: number
20+
currency?: string
21+
userAgent: string
22+
sourceUrl: string
23+
testEventCode?: string
24+
};
525

626
/**
727
* Facebook Conversion API Event Handler for Next.js.

src/handlers/event-handler.types.ts

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/services/server-side-events/server-side-events.ts renamed to src/services/server-side-events.ts

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,34 @@
11
import { FormData } from 'formdata-node';
2-
import graphApi from '../../api/graph';
3-
import { Arguments, Response } from './server-side-events.types';
4-
import { sha256Hash } from '../../utils/hash';
2+
import graphApi from '../api/graph-api';
3+
import { sha256Hash } from '../utils/hash';
4+
5+
type Arguments = {
6+
eventName: string
7+
eventId: string
8+
emails?: Array<string> | null
9+
phones?: Array<string> | null
10+
firstName?: string
11+
lastName?: string
12+
country?: string
13+
city?: string
14+
zipCode?: string
15+
products: {
16+
sku: string
17+
quantity: number
18+
}[]
19+
value?: number
20+
currency?: string
21+
fbp: string
22+
fbc: string
23+
ipAddress: string
24+
userAgent: string
25+
sourceUrl: string
26+
testEventCode?: string
27+
};
28+
29+
type Response = {
30+
events_received?: number
31+
};
532

633
/**
734
* Send server side event to Facebook Graph API.

src/services/server-side-events/index.ts

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)