Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

case on which you can see the issue with async operations inside handler #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,43 @@ import { track } from './track'
import { getOrderPayload, getPageviewPayload } from './payload'

export default function (manager: Manager, settings: ComponentSettings) {
manager.addEventListener('pageview', event => {
return track(event, getPageviewPayload(event, settings))
manager.addEventListener('pageview', async event => {
console.log('Event pageview: ' + JSON.stringify(event))

const res = await track(event, getPageviewPayload(event, settings))

console.log(`--> and here is another log we wont see`, 123)

console.log(
`--> this track() wont be invoked, at least i didnt see the log inside the fn second time`
)
const res2 = await track(event, getPageviewPayload(event, settings))

console.log(`--> no log for res2`, res2)

return res
})

manager.addEventListener('ecommerce', event => {
manager.addEventListener('ecommerce', async event => {
console.log('Event ecommerce: ' + JSON.stringify(event))
const action =
event.name || event.payload.name || event.payload.ecommerce.name

switch (action) {
case 'Product Viewed': {
return track(event, getPageviewPayload(event, settings))
const res = await track(event, getPageviewPayload(event, settings))
console.log('res', res)
return res
}

case 'Order Completed': {
return track(event, getOrderPayload(event, settings))
const res = await track(event, getOrderPayload(event, settings))
console.log('res', res)
return res
}

default: {
console.log('Wrong action', action)
return
}
}
Expand Down
19 changes: 14 additions & 5 deletions src/track.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,24 @@ import { MCEvent } from '@managed-components/types'
import { DemaPayload } from './types'

export const track = async (event: MCEvent, payload: DemaPayload) => {
console.log('Run track', event, payload)

const url = new URL('https://tracker.dema.ai/track.js')

for (const [key, value] of Object.entries(payload)) {
url.searchParams.set(key, value)
}

return fetch(url.toString(), {
headers: {
'User-Agent': event.client.userAgent,
},
})
// console.log('Track: ' + url.toString())

// let's use API that works 100% just for testing
console.log('Track:', 'jsonplaceholder')

// return fetch(url.toString(), {
return fetch('https://jsonplaceholder.typicode.com/posts')
.then(response => response.json())
.then(response => {
console.log('--> Here is the response we wont see', response)
return response
})
}