Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

load paddle.js only when user is authenticated

Blue Mouse538490a4b012
diff --git a/src/App.tsx b/src/App.tsx
index e5824fa8e..f4e5e6044 100644
--- a/src/App.tsx
+++ b/src/App.tsx
@@ -19,9 +19,10 @@ import Loader from 'ui/Loader'
import ScrollToTop from 'hoc/ScrollToTop'
import Selfhosted from 'hoc/Selfhosted'
import {
- isSelfhosted, // THEME_TYPE,
+ isSelfhosted, PADDLE_JS_URL, PADDLE_VENDOR_ID, // THEME_TYPE,
} from 'redux/constants'
import { getAccessToken } from 'utils/accessToken'
+import { loadScript } from 'utils/generic'
import { authActions } from 'redux/reducers/auth'
import sagaActions from 'redux/sagas/actions'
import { errorsActions } from 'redux/reducers/errors'
@@ -103,6 +104,7 @@ const App = () => {
const {
loading, authenticated, user,
} = useSelector((state: StateType) => state.auth)
+ const paddleLoaded = useSelector((state: StateType) => state.ui.misc.paddleLoaded)
const { theme } = useSelector((state: StateType) => state.ui.theme)
const { error } = useSelector((state: StateType) => state.errors)
const { message, type } = useSelector((state: StateType) => state.alerts)
@@ -110,7 +112,14 @@ const App = () => {
const accessToken = getAccessToken()
const refreshToken = getRefreshToken()
+ // Paddle (payment processor) set-up
useEffect(() => {
+ if (paddleLoaded || !authenticated) {
+ return
+ }
+
+ loadScript(PADDLE_JS_URL)
+
const eventCallback = (data: any) => {
dispatch(UIActions.setPaddleLastEvent(data))
}
@@ -122,13 +131,13 @@ const App = () => {
clearInterval(interval)
} else if ((window as any)?.Paddle) {
(window as any).Paddle.Setup({
- vendor: 139393,
+ vendor: PADDLE_VENDOR_ID,
eventCallback,
})
clearInterval(interval)
}
}
- }, []) // eslint-disable-line
+ }, [paddleLoaded, authenticated]) // eslint-disable-line
useEffect(() => {
const loaderEl = document.getElementById('loader')
diff --git a/src/pages/Billing/index.tsx b/src/pages/Billing/index.tsx
index 33ec34a24..28ef299b9 100644
--- a/src/pages/Billing/index.tsx
+++ b/src/pages/Billing/index.tsx
@@ -18,15 +18,6 @@ import Pricing from '../MainPage/Pricing'
dayjs.extend(utc)
dayjs.extend(duration)
-const loadPaddleScript = () => {
- const script = document.createElement('script')
- script.src = 'https://cdn.paddle.com/paddle/paddle.js'
- script.async = true
- document.body.appendChild(script)
-}
-
-loadPaddleScript()
-
const Billing = (): JSX.Element => {
const [isCancelSubModalOpened, setIsCancelSubModalOpened] = useState<boolean>(false)
const { user }: {
diff --git a/src/redux/constants/index.ts b/src/redux/constants/index.ts
index dc689b100..4413a42d8 100644
--- a/src/redux/constants/index.ts
+++ b/src/redux/constants/index.ts
@@ -124,6 +124,9 @@ export const TimeFormat: {
export const FREE_TIER_KEY: string = 'free'
+export const PADDLE_JS_URL = 'https://cdn.paddle.com/paddle/paddle.js'
+export const PADDLE_VENDOR_ID = 139393
+
// a dedicated variable is needed for paid tier checking
export const WEEKLY_REPORT_FREQUENCY: string = 'weekly'
export const reportFrequencies: string[] = [WEEKLY_REPORT_FREQUENCY, 'monthly', 'quarterly', 'never']
diff --git a/src/redux/reducers/ui/misc.ts b/src/redux/reducers/ui/misc.ts
index 627b0c32c..11ee6ad0e 100644
--- a/src/redux/reducers/ui/misc.ts
+++ b/src/redux/reducers/ui/misc.ts
@@ -5,11 +5,12 @@ import { secondsTillNextMonth } from 'utils/generic'
import { IStats } from 'redux/models/IStats'
interface IInitialState {
- stats: IStats
- paddle: any
- showNoEventsLeftBanner: boolean
- lastBlogPost: any
- extensions: any[]
+ stats: IStats
+ paddleLoaded: boolean
+ paddle: any
+ showNoEventsLeftBanner: boolean
+ lastBlogPost: any
+ extensions: any[]
}
const initialState: IInitialState = {
@@ -18,6 +19,7 @@ const initialState: IInitialState = {
projects: 0,
pageviews: 0,
},
+ paddleLoaded: false,
paddle: {},
showNoEventsLeftBanner: false,
lastBlogPost: null,
diff --git a/src/utils/generic.ts b/src/utils/generic.ts
index 1353cff4f..ac7416579 100644
--- a/src/utils/generic.ts
+++ b/src/utils/generic.ts
@@ -78,3 +78,10 @@ export const sumArrays = (...arrays: any): number[] => {
export const openBrowserWindow = (url: string, width: number, height: number) => {
return window.open(url, '', `width=${width},height=${height},top=${(window.innerHeight - height) / 2},left=${(window.innerWidth - width) / 2}`)
}
+
+export const loadScript = (url: string) => {
+ const script = document.createElement('script')
+ script.src = url
+ script.async = true
+ document.body.appendChild(script)
+}