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.
- Loading branch information
Showing
14 changed files
with
135 additions
and
107 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) { | ||
return console.warn( | ||
`Could not find element ${id}. Click event will not be send. This will cause loss in analytics`, | ||
); | ||
} | ||
|
||
element.addEventListener("click", () => { | ||
console.log(JSON.stringify(event, null, 2)); | ||
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; id?: string }, | ||
) => <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
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
Oops, something went wrong.