-
Notifications
You must be signed in to change notification settings - Fork 412
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1826 from filipedeschamps/feat/Umami-Analytics
Feat/umami analytics
- Loading branch information
Showing
9 changed files
with
427 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
CREATE DATABASE umami; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* eslint-disable no-console */ | ||
const { Client } = require('pg'); | ||
|
||
const endpoint = process.env.UMAMI_ENDPOINT; | ||
const websiteDomain = `${process.env.NEXT_PUBLIC_WEBSERVER_HOST}:${process.env.NEXT_PUBLIC_WEBSERVER_PORT}`; | ||
const websiteId = process.env.NEXT_PUBLIC_UMAMI_WEBSITE_ID; | ||
const connectionString = `postgres://${process.env.POSTGRES_USER}:${process.env.POSTGRES_PASSWORD}@${process.env.POSTGRES_HOST}:${process.env.POSTGRES_PORT}/${process.env.UMAMI_DB}`; | ||
const username = process.env.UMAMI_API_CLIENT_USERNAME || 'admin'; | ||
const password = process.env.UMAMI_API_CLIENT_PASSWORD || 'umami'; | ||
|
||
const client = new Client({ | ||
connectionString, | ||
connectionTimeoutMillis: 5000, | ||
idleTimeoutMillis: 30000, | ||
allowExitOnIdle: false, | ||
}); | ||
|
||
configUmami(); | ||
|
||
async function configUmami() { | ||
console.log('\n> Waiting for Umami Server to start...'); | ||
console.log('> Endpoint:', endpoint); | ||
|
||
await waitForServer(); | ||
|
||
console.log('> Creating Umami configuration...'); | ||
|
||
const token = await fetch(`${endpoint}/api/auth/login`, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({ | ||
username, | ||
password, | ||
}), | ||
}) | ||
.then((res) => res.json()) | ||
.then((data) => data.token); | ||
|
||
console.log('> Token:', token); | ||
|
||
const websites = await fetch(`${endpoint}/api/websites`, { | ||
headers: { | ||
Authorization: `Bearer ${token}`, | ||
}, | ||
}) | ||
.then((res) => res.json()) | ||
.then((data) => data.data); | ||
|
||
let existDevWebisite; | ||
|
||
if (websites.length) { | ||
existDevWebisite = websites.some((site) => site.id === websiteId); | ||
} | ||
|
||
await client.connect(); | ||
|
||
if (!existDevWebisite) { | ||
const website = await fetch(`${endpoint}/api/websites`, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
Authorization: `Bearer ${token}`, | ||
}, | ||
body: JSON.stringify({ | ||
name: 'TabNews Dev', | ||
domain: websiteDomain, | ||
}), | ||
}).then((res) => res.json()); | ||
|
||
await client.query('UPDATE website SET website_id = $1 WHERE website_id = $2;', [websiteId, website.id]); | ||
} | ||
|
||
await client.end(); | ||
|
||
console.log('> Umami configuration created!'); | ||
} | ||
|
||
async function waitForServer(attempts = 5) { | ||
try { | ||
return await fetch(`${endpoint}/api/heartbeat`); | ||
} catch (error) { | ||
if (attempts > 1) { | ||
console.log('> Umami is not ready, waiting...'); | ||
await new Promise((resolve) => setTimeout(resolve, 1000)); | ||
return waitForServer(attempts - 1); | ||
} | ||
|
||
console.error('🔴 Umami is not ready, exiting...'); | ||
process.exit(1); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { Analytics as VercelAnalytics } from '@vercel/analytics/react'; | ||
import Script from 'next/script'; | ||
|
||
export default function Analytics() { | ||
return ( | ||
<> | ||
<Script | ||
id="umami-script" | ||
src="/analytics.js" | ||
data-website-id={process.env.NEXT_PUBLIC_UMAMI_WEBSITE_ID} | ||
data-path-matcher="^(/login|/cadastro|/publicar)$" | ||
data-exclude-search="true" | ||
strategy="lazyOnload" | ||
/> | ||
<VercelAnalytics | ||
beforeSend={(event) => { | ||
const { pathname } = new URL(event.url); | ||
if (['/', '/publicar'].includes(pathname)) { | ||
return null; | ||
} | ||
return event; | ||
}} | ||
/> | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.