-
-
Notifications
You must be signed in to change notification settings - Fork 15
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
36ea694
commit ec9d576
Showing
5 changed files
with
120 additions
and
94 deletions.
There are no files selected for viewing
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,21 +1,58 @@ | ||
export const purchase = [ | ||
'event', | ||
'purchase', | ||
{ | ||
data_id: '0rd3r1d', | ||
data_currency: 'EUR', | ||
data_shipping: 5.22, | ||
data_taxes: 73.76, | ||
data_total: 555, | ||
transaction_id: '0rd3r1d', | ||
value: 555, | ||
tax: 73.76, | ||
shipping: 5.22, | ||
currency: 'EUR', | ||
items: [ | ||
{ item_id: 'ers', item_name: 'Everyday Ruck Snack', quantity: 1 }, | ||
{ item_id: 'cc', item_name: 'Cool Cap', quantity: 1 }, | ||
], | ||
send_to: 'G-XXXXXX-1', | ||
}, | ||
]; | ||
import type { DestinationGoogleGA4 } from '../src'; | ||
import { getEvent } from '@elbwalker/utils'; | ||
|
||
const customDefault: DestinationGoogleGA4.Custom = { | ||
measurementId: 'G-XXXXXX-1', | ||
}; | ||
|
||
function useCustom(custom: DestinationGoogleGA4.Custom = customDefault) { | ||
return { | ||
send_to: custom.measurementId, | ||
}; | ||
} | ||
|
||
export function purchase(custom: DestinationGoogleGA4.Custom = customDefault) { | ||
const event = getEvent('order complete'); | ||
const product1 = event.nested[0].data; | ||
const product2 = event.nested[1].data; | ||
|
||
return [ | ||
'event', | ||
'purchase', | ||
{ | ||
transaction_id: event.data.id, | ||
value: event.data.total, | ||
tax: event.data.taxes, | ||
shipping: event.data.shipping, | ||
currency: 'EUR', | ||
items: [ | ||
{ item_id: product1.id, item_name: product1.name, quantity: 1 }, | ||
{ item_id: product2.id, item_name: product2.name, quantity: 1 }, | ||
], | ||
...useCustom(custom), | ||
}, | ||
]; | ||
} | ||
|
||
export function add_to_cart( | ||
custom: DestinationGoogleGA4.Custom = customDefault, | ||
) { | ||
const event = getEvent('product add'); | ||
|
||
return [ | ||
'event', | ||
'add_to_cart', | ||
{ | ||
currency: 'EUR', | ||
value: event.data.price, | ||
items: [ | ||
{ | ||
item_id: event.data.id, | ||
item_variant: event.data.color, | ||
quantity: 1, | ||
}, | ||
], | ||
...useCustom(custom), | ||
}, | ||
]; | ||
} |
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,2 +1,2 @@ | ||
export * as events from './events'; | ||
export { mapping } from './mapping'; | ||
export * as mapping from './mapping'; |
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,33 +1,58 @@ | ||
import type { Mapping } from '@elbwalker/types'; | ||
import type { DestinationGoogleGA4 } from '../src'; | ||
import { isObject } from '@elbwalker/utils'; | ||
|
||
export const mapping = { | ||
order: { | ||
complete: { | ||
name: 'purchase', | ||
data: { | ||
map: { | ||
transaction_id: 'data.id', | ||
value: 'data.total', | ||
tax: 'data.taxes', | ||
shipping: 'data.shipping', | ||
currency: { key: 'data.currency', value: 'EUR' }, | ||
items: { | ||
loop: [ | ||
'nested', | ||
{ | ||
condition: (entity) => | ||
isObject(entity) && entity.type === 'product', | ||
map: { | ||
item_id: 'data.id', | ||
item_name: 'data.name', | ||
quantity: { key: 'data.quantity', value: 1 }, | ||
}, | ||
}, | ||
], | ||
export const purchase: DestinationGoogleGA4.EventConfig = { | ||
name: 'purchase', | ||
data: { | ||
map: { | ||
transaction_id: 'data.id', | ||
value: 'data.total', | ||
tax: 'data.taxes', | ||
shipping: 'data.shipping', | ||
currency: { key: 'data.currency', value: 'EUR' }, | ||
items: { | ||
loop: [ | ||
'nested', | ||
{ | ||
condition: (entity) => | ||
isObject(entity) && entity.type === 'product', | ||
map: { | ||
item_id: 'data.id', | ||
item_name: 'data.name', | ||
quantity: { key: 'data.quantity', value: 1 }, | ||
}, | ||
}, | ||
}, | ||
], | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
export const add_to_cart: DestinationGoogleGA4.EventConfig = { | ||
name: 'add_to_cart', | ||
data: { | ||
map: { | ||
currency: { value: 'EUR', key: 'data.currency' }, | ||
override: 'data.old', | ||
value: 'data.price', | ||
items: { | ||
loop: [ | ||
'this', | ||
{ | ||
map: { | ||
item_id: 'data.id', | ||
item_variant: 'data.color', | ||
quantity: { value: 1, key: 'data.quantity' }, | ||
}, | ||
}, | ||
], | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
export const config = { | ||
order: { complete: purchase }, | ||
product: { add: add_to_cart }, | ||
} satisfies Mapping.Config; |
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