Skip to content

Commit

Permalink
Retry broadcaster failures, small website optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
StatsProIO committed Jul 28, 2024
1 parent 7d00e6f commit c7f7e82
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 39 deletions.
60 changes: 33 additions & 27 deletions src/public/js/broadcaster.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,41 @@ var endpoint = parsedScriptUrl.protocol + "//" + parsedScriptUrl.hostname + "/ap
var timeOnPageEndpoint = parsedScriptUrl.protocol + "//" + parsedScriptUrl.hostname + "/api/event/time-on-page";
var errorEndpoint = parsedScriptUrl.protocol + "//" + parsedScriptUrl.hostname + "/api/error";

var isShortLinkRedirect = scriptEl.getAttribute('data-short-link-id') != null;
var shortLinkId = scriptEl.getAttribute('data-short-link-id');
var shortLinkUrl = scriptEl.getAttribute('data-short-link-url');
/**
* Fetch, retrying on any failures
* On any response (100-599), returns the response
* On exceptions, retries
*/
const fetchPlus = (url, body, options = {}, retries) => {
let req = new Request(url, {
method: 'post',
headers: {
"Content-type": "application/json"
},
body: JSON.stringify(body)
});

return fetch(req, options)
.then((response) => {
//TODO: in the future, retry based on status code errors?
return response;
})
.catch((error) => {
if (retries > 0) {
return fetchPlus(url, body, options, retries - 1)
} else {
throw error;
}
});
}

function buildPageviewPayload() {
var payload = {};
payload.event_name = isShortLinkRedirect ? 'short-link-open' : 'pageview';
payload.event_name = 'pageview';
payload.location_href = location.href;
payload.location_host = window.location.host;
payload.location_pathname = window.location.pathname;
payload.domain = scriptEl.getAttribute('data-domain');
payload.short_link_id = shortLinkId;
payload.referrer = document.referrer || null;
payload.inner_width = window.innerWidth;
payload.lang = window.navigator.language || '';
Expand Down Expand Up @@ -62,16 +85,7 @@ function queryParams() {
}

function sendRequest(url, body, next) {

let req = new Request(url, {
method: 'post',
headers: {
"Content-type": "application/json"
},
body: JSON.stringify(body)
});

fetch(req)
fetchPlus(url, body, {}, 5)
.then(function (response) {
if (response.status >= 200 && response.status < 300) {
return response.json();
Expand All @@ -82,20 +96,12 @@ function sendRequest(url, body, next) {
}
})
.then(function (responseJson) {
if (responseJson) {
if (isShortLinkRedirect) {
window.location.href = shortLinkUrl;
} else if (typeof next === 'function') {
next(responseJson)
}
if (responseJson && typeof next === 'function') {
next(responseJson)
}
}).catch(function (err) {
recordError({ message: 'Broadcaster request failed: ' + err.toString(), url, body });

if (isShortLinkRedirect) {
window.location.href = shortLinkUrl;
}
});
recordError({ message: 'Broadcaster request failed: ' + err.toString(), url, body });
});
}

function scheduleReoccringRequests(initialRequestJsonResponse) {
Expand Down
1 change: 0 additions & 1 deletion src/resources/js/Components/LocationChart.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ export function LocationChart({ inputData }) {
pressed: { outline: "none" },
}}
onMouseEnter={() => {
console.log("Mouse enter" + geo.properties.name);
setContent(`${geo.properties.name} - ${d["count"]}`);
}}
onMouseLeave={() => {
Expand Down
11 changes: 5 additions & 6 deletions src/resources/js/Pages/Home.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,10 @@ export default function Home(props) {
</AvatarGroup>
</Grid>
<Grid item xs={12} sx={{mt: 1}}>
<Typography variant={'subtitle2'} >Rated 4.3 by over 80 users!</Typography>
<Typography variant={'subtitle2'} >Rated 4.3 by over 250 users!</Typography>
<Rating size="large"
name="simple-controlled"
color={'white'}
border
defaultValue={4.3}
value={4.3}
emptyIcon={<StarBorderIcon sx={{color: 'white'}} fontSize={'30px'}/> }
Expand Down Expand Up @@ -162,7 +161,7 @@ export default function Home(props) {
<source srcSet={asset("/images/sample-dashboard.webp")} media="(min-width: 800px)" />
<img fetchpriority="high" className={'demo-image'} src={asset("/images/sample-dashboard-m.webp")} style={{borderRadius: '10px'}} width={"100%"} alt={'Shows a demo of statspro'}/>
</picture>
<div class="demo-image-overlay">
<div className="demo-image-overlay">
<Typography variant={'h4'} color={'white'} className={'live-demo-text'}>Click for Live Demo</Typography>
</div>
</div>
Expand Down Expand Up @@ -232,11 +231,11 @@ export default function Home(props) {

<Grid item sm={12} md={8} lg={6} >
{trackingFeatures.map((trackingFeature) => {
return <Chip label={trackingFeature} sx={{m: 1, fontSize: '18px'}} size={'large'} color={'primary'} variant="outlined" />
return <Chip key={trackingFeature} label={trackingFeature} sx={{m: 1, fontSize: '18px'}} size={'large'} color={'primary'} variant="outlined" />
})}

{newTrackingFeatures.map((newTrackingFeature) => {
return <Chip label={'[NEW!] ' + newTrackingFeature} sx={{m: 1, fontSize: '18px'}} size={'large'} color={'success'} variant="outlined" />
return <Chip key={newTrackingFeature} label={'[NEW!] ' + newTrackingFeature} sx={{m: 1, fontSize: '18px'}} size={'large'} color={'success'} variant="outlined" />
})}
</Grid>
<Grid item sm={12} md={12} lg={12}>
Expand Down Expand Up @@ -384,7 +383,7 @@ export default function Home(props) {
<Grid item xs={12} sm={12} md={12} lg={10}>
<Grid container>
{faqs.map((faq => (
<Grid item sm={12} sm={6} md={4} >
<Grid key={faq.title} item sm={12} sm={6} md={4} >
<Paper elevation={1} sx={{ m: 2}} style={{boxShadow: 'rgba(149, 157, 165, 0.2) 0px 8px 24px'}}>
<Box sx={{ borderRadius: '5px', p: 3 }}>
<Stack direction="column" alignItems="center" justifyContent="center" gap={1}>
Expand Down
6 changes: 1 addition & 5 deletions src/resources/js/app.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,13 @@ import '../css/app.css';
import React from 'react';
import {render} from 'react-dom';
import {createInertiaApp} from '@inertiajs/inertia-react';
import {InertiaProgress} from '@inertiajs/progress';
import {resolvePageComponent} from 'laravel-vite-plugin/inertia-helpers';

const appName = window.document.getElementsByTagName('title')[0]?.innerText || 'Laravel';

createInertiaApp({
title: (title) => `${title} - ${appName}`,
title: (title) => `${title} - StatsPro`,
resolve: (name) => resolvePageComponent(`./Pages/${name}.jsx`, import.meta.glob('./Pages/**/*.jsx')),
setup({ el, App, props }) {
return render(<App {...props} />, el);
},
});

InertiaProgress.init({ color: 'blue' });

0 comments on commit c7f7e82

Please sign in to comment.