Skip to content

Commit

Permalink
feat: update pixel mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-phan committed Nov 27, 2023
1 parent 7cdf0c4 commit a568dec
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 94 deletions.
17 changes: 8 additions & 9 deletions packages/hydrogen/src/WeaverseHydrogenRoot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@ import { WeaverseRoot } from '@weaverse/react'
import React, { memo, Suspense } from 'react'
import { ErrorBoundary } from 'react-error-boundary'

import { ThemeProvider, createWeaverseInstance } from './context'
import { useStudio } from './hooks/use-studio'
import { createWeaverseInstance, ThemeProvider } from './context'
import { ThemeSettingsStore } from './hooks/use-theme-settings'
import type {
HydrogenComponent,
RootRouteData,
WeaverseHydrogenRootProps,
WeaverseLoaderData,
} from './types'

import { WeaversePixel } from '~/pixel'
import { WeaverseEffect } from '~/effect'

type WeaverseData = WeaverseLoaderData | Promise<WeaverseLoaderData>

Expand Down Expand Up @@ -58,11 +56,12 @@ function RenderRoot(props: {
}) {
let { data, components } = props
let weaverse = createWeaverseInstance(data, components)
useStudio(weaverse)
return <>
<WeaverseRoot context={weaverse} />
<WeaversePixel context={weaverse} />
</>
return (
<>
<WeaverseRoot context={weaverse} />
<WeaverseEffect context={weaverse} />
</>
)
}

export function withWeaverse(Component: React.ComponentType<any>) {
Expand Down
17 changes: 17 additions & 0 deletions packages/hydrogen/src/effect.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// create a pixel Component that will be used to track page views
// /api/public/weaverse-pixel?projectId=xxx&pageId=xxx
// The pixel will be removed from the DOM after it is loaded

import { memo } from 'react'

import type { WeaverseHydrogen } from './index'
import { useStudio } from '~/hooks/use-studio'
import { usePixel } from '~/hooks/use-pixel'

export let WeaverseEffect = memo(
({ context }: { context: WeaverseHydrogen }) => {
useStudio(context)
usePixel(context)
return null
},
)
21 changes: 21 additions & 0 deletions packages/hydrogen/src/hooks/use-pixel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/* eslint-disable react-hooks/exhaustive-deps */
import type {WeaverseHydrogen} from '~/index'
import {useEffect} from 'react'
import {useLocation} from '@remix-run/react'
let fetchingKey = ''
export let usePixel = (context: WeaverseHydrogen) => {
// TODO: create a post xhr request to the pixel endpoint
let { projectId, pageId, weaverseHost, isDesignMode } = context
const location = useLocation();
useEffect(() => {
if (isDesignMode || !projectId || !pageId || !weaverseHost) return
let currentKey = `${projectId}-${pageId}-${location.pathname}`
if (fetchingKey === currentKey) return
fetchingKey = currentKey
let url = `${weaverseHost}/api/public/px`
let xhr = new XMLHttpRequest()
xhr.open('POST', url, true)
xhr.setRequestHeader('Content-Type', 'application/json')
xhr.send(JSON.stringify({ projectId, pageId }))
}, [location])
}
1 change: 0 additions & 1 deletion packages/hydrogen/src/hooks/use-studio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ export function useStudio(weaverse: WeaverseHydrogen) {
let themeSettingsStore = useThemeContext()
let { isDesignMode, weaverseHost, weaverseVersion } = weaverse
let isStudio = isIframe && isDesignMode && weaverseHost && weaverseVersion

useEffect(() => {
if (isStudio) {
weaverse.internal = {
Expand Down
25 changes: 0 additions & 25 deletions packages/hydrogen/src/pixel.tsx

This file was deleted.

79 changes: 21 additions & 58 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion templates/pilot

0 comments on commit a568dec

Please sign in to comment.