generated from deco-sites/fashion
-
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.
Merge pull request #66 from deco-sites/fix/events
fix: Events and remove Partytown
- Loading branch information
Showing
19 changed files
with
198 additions
and
152 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,34 +1,63 @@ | ||
import { sendEvent } from "$store/sdk/analytics.tsx"; | ||
import type { AnalyticsEvent } from "apps/commerce/types.ts"; | ||
import { scriptAsDataURI } from "apps/utils/dataURI.ts"; | ||
|
||
const snippet = (id: string, event: AnalyticsEvent) => { | ||
const element = document.getElementById(id); | ||
|
||
if (!element) { | ||
console.warn( | ||
`Could not find element ${id}. Click event will not be send. This will cause loss in analytics`, | ||
); | ||
} else { | ||
element.addEventListener( | ||
"click", | ||
() => window.DECO.events.dispatch(event), | ||
); | ||
} | ||
}; | ||
|
||
/** | ||
* This function is usefull for sending events on click. Works with both Server and Islands components | ||
*/ | ||
export const SendEventOnClick = <E extends AnalyticsEvent>({ event, id }: { | ||
event: E; | ||
id: string; | ||
}) => <script defer src={scriptAsDataURI(snippet, id, event)} />; | ||
}) => ( | ||
<script | ||
defer | ||
src={scriptAsDataURI( | ||
(id: string, event: AnalyticsEvent) => { | ||
const elem = document.getElementById(id); | ||
|
||
/** | ||
* This componente should be used when want to send event for rendered componentes. | ||
* This behavior is usefull for view_* events. | ||
*/ | ||
export const SendEventOnLoad = <E extends AnalyticsEvent>( | ||
{ event }: { event: E }, | ||
) => <script defer src={scriptAsDataURI(sendEvent, event)} />; | ||
if (!elem) { | ||
return console.warn( | ||
`Could not find element ${id}. Click event will not be send. This will cause loss in analytics`, | ||
); | ||
} | ||
|
||
elem.addEventListener("click", () => { | ||
window.DECO.events.dispatch(event); | ||
}); | ||
}, | ||
id, | ||
event, | ||
)} | ||
/> | ||
); | ||
|
||
export const SendEventOnView = <E extends AnalyticsEvent>( | ||
{ event, id }: { event: E; id: string }, | ||
) => ( | ||
<script | ||
defer | ||
src={scriptAsDataURI( | ||
(id: string, event: E) => { | ||
const elem = document.getElementById(id); | ||
|
||
if (!elem) { | ||
return console.warn( | ||
`Could not find element ${id}. Click event will not be send. This will cause loss in analytics`, | ||
); | ||
} | ||
|
||
const observer = new IntersectionObserver((items) => { | ||
for (const item of items) { | ||
if (!item.isIntersecting) continue; | ||
|
||
window.DECO.events.dispatch(event); | ||
observer.unobserve(elem); | ||
} | ||
}); | ||
|
||
observer.observe(elem); | ||
}, | ||
id, | ||
event, | ||
)} | ||
/> | ||
); |
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
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,18 +1,20 @@ | ||
import { useCart } from "apps/shopify/hooks/useCart.ts"; | ||
import Button, { Props as BtnProps } from "./common.tsx"; | ||
|
||
export type Props = Omit<BtnProps, "onAddItem" | "platform">; | ||
export type Props = Omit<BtnProps, "onAddItem"> & { | ||
productID: string; | ||
}; | ||
|
||
function AddToCartButton(props: Props) { | ||
function AddToCartButton({ productID, eventParams }: Props) { | ||
const { addItems } = useCart(); | ||
const onAddItem = () => | ||
addItems({ | ||
lines: { | ||
merchandiseId: props.productID, | ||
merchandiseId: productID, | ||
}, | ||
}); | ||
|
||
return <Button onAddItem={onAddItem} {...props} />; | ||
return <Button onAddItem={onAddItem} eventParams={eventParams} />; | ||
} | ||
|
||
export default AddToCartButton; |
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,22 +1,23 @@ | ||
import { useCart } from "apps/vtex/hooks/useCart.ts"; | ||
import Button, { Props as BtnProps } from "./common.tsx"; | ||
|
||
export interface Props extends Omit<BtnProps, "onAddItem" | "platform"> { | ||
export interface Props extends Omit<BtnProps, "onAddItem"> { | ||
seller: string; | ||
productID: string; | ||
} | ||
|
||
function AddToCartButton(props: Props) { | ||
function AddToCartButton({ seller, productID, eventParams }: Props) { | ||
const { addItems } = useCart(); | ||
const onAddItem = () => | ||
addItems({ | ||
orderItems: [{ | ||
id: props.productID, | ||
seller: props.seller, | ||
id: productID, | ||
seller: seller, | ||
quantity: 1, | ||
}], | ||
}); | ||
|
||
return <Button onAddItem={onAddItem} {...props} />; | ||
return <Button onAddItem={onAddItem} eventParams={eventParams} />; | ||
} | ||
|
||
export default AddToCartButton; |
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,19 +1,19 @@ | ||
import { useCart } from "apps/wake/hooks/useCart.ts"; | ||
import Button, { Props as BtnProps } from "./common.tsx"; | ||
|
||
// deno-lint-ignore no-empty-interface | ||
export interface Props extends Omit<BtnProps, "onAddItem" | "platform"> { | ||
export interface Props extends Omit<BtnProps, "onAddItem"> { | ||
productID: string; | ||
} | ||
|
||
function AddToCartButton(props: Props) { | ||
function AddToCartButton({ productID, eventParams }: Props) { | ||
const { addItem } = useCart(); | ||
const onAddItem = () => | ||
addItem({ | ||
productVariantId: Number(props.productID), | ||
productVariantId: Number(productID), | ||
quantity: 1, | ||
}); | ||
|
||
return <Button onAddItem={onAddItem} {...props} />; | ||
return <Button onAddItem={onAddItem} eventParams={eventParams} />; | ||
} | ||
|
||
export default AddToCartButton; |
Oops, something went wrong.