-
-
Save shayanabbas/c1433ec8e0a377dceca3b206fd742ab7 to your computer and use it in GitHub Desktop.
Make sure to replace the placeholders 'YOUR_MATOMO_SERVER', 'YOUR_SITE_ID', 'YOUR_AUTH_TOKEN', '[email protected]', '[email protected]', 'your_smtp_host', 'your_smtp_username', and 'your_smtp_password' with the actual values specific to your setup.
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
const fs = require('fs'); | |
const request = require('request'); | |
const csvWriter = require('csv-writer').createObjectCsvWriter; | |
const nodemailer = require('nodemailer'); | |
// Matomo API endpoint and parameters | |
const apiUrl = 'https://your-matomo-server.com/matomo.php'; | |
const siteId = 'YOUR_SITE_ID'; | |
const tokenAuth = 'YOUR_AUTH_TOKEN'; | |
const dateRange = 'previous30'; // Adjust the date range as needed | |
const apiParams = { | |
module: 'API', | |
method: 'API.get', | |
idSite: siteId, | |
period: 'day', | |
date: dateRange, | |
format: 'json', | |
token_auth: tokenAuth, | |
apiModule: 'Actions', | |
apiAction: 'getPageTitles' | |
}; | |
// Fetch data from Matomo API | |
request.get({ url: apiUrl, qs: apiParams }, (error, response, body) => { | |
if (error) { | |
console.error('Error fetching data from Matomo API:', error); | |
return; | |
} | |
const data = JSON.parse(body); | |
// Process data and prepare CSV | |
const processedData = data.map((row) => ({ | |
'Page Title': row.label, | |
Visits: row.nb_visits | |
})); | |
// Generate CSV file | |
const csvWriterObject = csvWriter({ | |
path: 'matomo_data.csv', | |
header: [ | |
{ id: 'Page Title', title: 'Page Title' }, | |
{ id: 'Visits', title: 'Visits' } | |
] | |
}); | |
csvWriterObject | |
.writeRecords(processedData) | |
.then(() => { | |
console.log('CSV file generated successfully!'); | |
// Email configuration | |
const transporter = nodemailer.createTransport({ | |
host: 'your_smtp_host', | |
port: 587, | |
secure: false, | |
auth: { | |
user: 'your_smtp_username', | |
pass: 'your_smtp_password' | |
} | |
}); | |
const mailOptions = { | |
from: '[email protected]', | |
to: '[email protected]', | |
subject: 'Matomo Data Report', | |
text: 'Please find the attached Matomo data report', | |
attachments: [ | |
{ | |
filename: 'matomo_data.csv', | |
path: 'matomo_data.csv' | |
} | |
] | |
}; | |
// Send email | |
transporter.sendMail(mailOptions, (error, info) => { | |
if (error) { | |
console.error('Error sending email:', error); | |
} else { | |
console.log('Email sent successfully!'); | |
} | |
// Remove CSV file after sending email | |
fs.unlink('matomo_data.csv', (error) => { | |
if (error) { | |
console.error('Error deleting CSV file:', error); | |
} else { | |
console.log('CSV file deleted successfully!'); | |
} | |
}); | |
}); | |
}) | |
.catch((error) => { | |
console.error('Error writing CSV file:', error); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment