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

Refactor: script injectors #39

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft

Conversation

axe312ger
Copy link
Collaborator

There is no need to wrap the whole application with our integration components, thats why we applied #33

This PR refactors the code so it reflects what these integrations actually do:

  • Inject Scripts

It also fixes a bug that prevents multiple WrapperComponents to render.

window.setTimeout(() => {
trackPageView(location)
redBoxLtdTracker && redBoxLtdTracker.push(['page', location.pathname])
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

relevant

</svg>
)

const ScriptInjector: React.FC = () => {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this example injects a tracking pixel

@@ -34,63 +25,55 @@ const Icon: React.FC = () => (
</svg>
)

// ensure that the tracker script will be initialized once in runtime
let wasInitialized = false
const ScriptInjector: React.FC = () => {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this integration injects a script tag and manipulates window as many tracking scripts do

interface RedBoxLtdTracker extends Array<Array<String>>, Tracker {}

// For usage non-react context when tracking page views (Docusaurus, Gatsby, ...)
// @todo we need to save-guard this by checking if integration is actually enabled
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@todo 🤔

}
React.useEffect(() => {
if (isEnabled && !tracker) {
locateTracker('rbltd', setTracker)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

relevant, this ensures react can track to the correct object as soon its available

@@ -17,6 +17,7 @@
"@types/react": "^16.9.11",
"@types/react-dom": "^16.8.4",
"parcel": "1.12.3",
"parcel-plugin-static-files-copy": "^2.6.0",
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

relevant, copies our red-box-ltd.js and pixel.png into the example public dir

Boolean(i.ScriptInjector)
)
.map(({ ScriptInjector, id }) => <ScriptInjector key={id} />)
}, [config, decisions, isMounted])
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

relevant, this is the bug fix for multiple wrapper/injector components

export type Status = 'idle' | 'loading' | 'ready' | 'error'
export type ScriptElt = HTMLScriptElement | null

export function useScript(
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

relevant, injects the script tag and removes it when dismounted

return status
}

export function locateTracker(
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

relevant, finds our tracker from the external script in the window object, to ensure user userTracker() hook returns the proper tracker

@@ -65,8 +65,8 @@ export const IntegrationProfile: React.FC<IntegrationProfileProps> = ({
</td>
</tr>
<tr>
<td>WrapperComponent</td>
<td>{WrapperComponent ? '✅' : '⛔️'}</td>
<td>Injects &lt;script/&gt; tag</td>
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes now more sense :)

@@ -0,0 +1,33 @@
module.exports = {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

relevant, our first eslint rule.

This ensures people don't create integrations using document.createElement('script') to inject script tags. They should use our helper.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant