-
Notifications
You must be signed in to change notification settings - Fork 412
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
Feat/umami analytics #1826
Merged
Merged
Feat/umami analytics #1826
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isso aqui funcionaria, certo?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Não, pois não tem o mesmo valor. São bancos diferentes no mesmo servidor.