Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Parse logs for Matomo (Piwik) on Windows [using Powershell]
#Based on https://matomo.org/docs/log-analytics-tool-how-to/
#------------------------------------------------------------------------
# Config
#:: Python URL (must be 2.x)
$PY_BIN="C:\Program Files\Python\Python27\python.exe"
#:: Matomo import_logs executable
$IMPORT_LOGS="[path_to_piwik]\piwik\misc\log-analytics\import_logs.py"
#:: IIS log dir
$LOGS_DIR="C:\inetpub\logs\LogFiles"
#:: Access token from Piwik config (check admin panel)
$TOKEN="*****************"
#:: URL to your Piwik installation
$URL="http://website.com/piwik/"
# CPU threads
$THREADS=2
#matomo.json extraction
$sites=(Get-Content -Raw -Path .\sites.json | ConvertFrom-Json)
#.json file example:
#[
# {"DIR":"Directory1","ID":"1"},
# {"DIR":"Directory2","ID":"2"},
# {"DIR":"Directory3","ID":"3"}
#]
# Run command
foreach ($site in $sites)
{
# Automatically generated vars
$LOG_FILE="u_ex" + (Get-Date -UFormat "%y%m%d") + ".log"
$LOG_PATH="$LOGS_DIR\" + ($site).DIR + "\$LOG_FILE"
$PY_ARGs=" $IMPORT_LOGS --url=$URL --idsite=" + ($site).ID + " --token-auth=$TOKEN --recorders=$THREADS --enable-http-errors --enable-http-redirects --enable-static --enable-bots $LOG_PATH"
Start-Process $PY_BIN $PY_ARGs -wait
# Uncomment for debug:
# $PY_ARGs
Write-Host "Parsed log file: $LOG_PATH"
}
[
{"id":3, "dir":"W3SVC3", "name":"website.com"}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.