Skip to content

Commit

Permalink
chore: Dynamically add/remove albacross (#2825)
Browse files Browse the repository at this point in the history
  • Loading branch information
novakzaballa authored Oct 4, 2023
1 parent 6967be3 commit c252289
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 7 deletions.
1 change: 1 addition & 0 deletions docs/docs/deployment/hosting/locally-frontend.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ Current variables used between 'frontend/environment.js' and 'frontend/common/pr
- `AMPLITUDE_API_KEY`: The Amplitude key to use for behaviour tracking.
- `MIXPANEL_API_KEY`: Mixpanel analytics key to use for behaviour tracking.
- `SENTRY_API_KEY`: Sentry key for error reporting.
- `ALBACROSS_CLIENT_ID`: Albacross client ID key for behaviour tracking.
- `STATIC_ASSET_CDN_URL`: Used for replacing local static paths with a cdn, .e.g https://cdn.flagsmith.com. Defaults to
`/`, i.e. no CDN.
- `BASE_URL`: Used for specifying a base url path that's ignored during routing if serving from a subdirectory.
Expand Down
1 change: 1 addition & 0 deletions frontend/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ app.get('/config/project-overrides', (req, res) => {
name: 'hideInviteLinks',
value: envToBool('DISABLE_INVITE_LINKS', false),
},
{ name: 'albacross', value: process.env.ALBACROSS_CLIENT_ID },
]
let output = values.map(getVariable).join('')
let dynatrace = ''
Expand Down
74 changes: 67 additions & 7 deletions frontend/web/components/pages/HomePage.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import Icon from 'components/Icon'
import ErrorMessage from 'components/ErrorMessage'
import Button from 'components/base/forms/Button'

let controller = new AbortController()

const HomePage = class extends React.Component {
static contextTypes = {
router: propTypes.object.isRequired,
Expand All @@ -27,6 +29,42 @@ const HomePage = class extends React.Component {
}
}

addAlbacross() {
EventTarget.prototype.oldAddEventListener =
EventTarget.prototype.addEventListener
window._nQc = Project.albacross
const script = document.createElement('script')
script.type = 'text/javascript'
script.async = true
script.src = 'https://serve.albacross.com/track.js'
script.id = 'albacross'

EventTarget.prototype.addEventListener = function (
type,
listener,
options = false,
) {
const parsedOptions =
typeof options == 'boolean' ? { capture: options } : options

EventTarget.prototype.oldAddEventListener.call(this, type, listener, {
signal: controller.signal,
...parsedOptions,
})
}

document.body.appendChild(script)
}

removeAlbacross() {
controller.abort()
controller = new AbortController()
EventTarget.prototype.addEventListener =
EventTarget.prototype.oldAddEventListener
document.body.removeChild(document.getElementById('albacross'))
delete window._nQc
}

componentDidUpdate(prevProps) {
if (this.props.location.pathname !== prevProps.location.pathname) {
const emailField =
Expand All @@ -35,10 +73,31 @@ const HomePage = class extends React.Component {
if (emailField) {
emailField.focus()
}
if (
Project.albacross &&
this.props.location.pathname.indexOf('signup') !== -1
) {
this.addAlbacross()
} else if (document.getElementById('albacross')) {
this.removeAlbacross()
}
}
}

componentWillUnmount() {
if (document.getElementById('albacross')) {
this.removeAlbacross()
}
}

componentDidMount() {
if (
Project.albacross &&
this.props.location.pathname.indexOf('signup') !== -1
) {
this.addAlbacross()
}

document.body.classList.remove('dark')
if (document.location.href.includes('oauth')) {
const parts = location.href.split('oauth/')
Expand Down Expand Up @@ -618,14 +677,15 @@ const HomePage = class extends React.Component {
</Card>
<Row className='justify-content-center'>
Have an account?{' '}
<Link
id='existing-member-btn'
to={`/login${redirect}`}
<Button
theme='text'
className='ml-1 fw-bold'
onClick={() => {
window.location.href = `/login${redirect}`
}}
>
<Button theme='text' className='ml-1 fw-bold'>
Log in
</Button>
</Link>
Log in
</Button>
</Row>
</React.Fragment>
)}
Expand Down

3 comments on commit c252289

@vercel
Copy link

@vercel vercel bot commented on c252289 Oct 4, 2023

Choose a reason for hiding this comment

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

@vercel
Copy link

@vercel vercel bot commented on c252289 Oct 4, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

docs – ./docs

docs-git-main-flagsmith.vercel.app
docs-flagsmith.vercel.app
docs.bullet-train.io
docs.flagsmith.com

@vercel
Copy link

@vercel vercel bot commented on c252289 Oct 4, 2023

Choose a reason for hiding this comment

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

Please sign in to comment.