-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathAddMatomoAnalytics.groovy
More file actions
96 lines (84 loc) · 3.44 KB
/
AddMatomoAnalytics.groovy
File metadata and controls
96 lines (84 loc) · 3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
@Grab(group='org.codehaus.gpars', module='gpars', version='1.2.1')
import groovyx.gpars.GParsPool
import java.util.concurrent.atomic.AtomicInteger
// Define the Matomo tracking code to insert
def matomoCode = ''' <!-- Matomo -->
<script>
var _paq = window._paq = window._paq || [];
/* tracker methods like "setCustomDimension" should be called before "trackPageView" */
_paq.push(["setDoNotTrack", true]);
_paq.push(["disableCookies"]);
_paq.push(['trackPageView']);
_paq.push(['enableLinkTracking']);
(function() {
var u="https://analytics.apache.org/";
_paq.push(['setTrackerUrl', u+'matomo.php']);
_paq.push(['setSiteId', '79']);
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s);
})();
</script>
<!-- End Matomo Code -->
'''
// Get the current directory
def currentDir = new File('.')
// Find all .html and .htm files recursively
def htmlFiles = []
currentDir.eachFileRecurse { file ->
if (file.name.endsWith('.html') || file.name.endsWith('.htm')) {
htmlFiles << file
}
}
println "Found ${htmlFiles.size()} HTML files"
// Thread-safe counters
def updatedCount = new AtomicInteger(0)
def skippedCount = new AtomicInteger(0)
// Get number of available processors
def numThreads = Runtime.runtime.availableProcessors()
println "Processing files using ${numThreads} threads with GPars..."
// Process files in parallel using GPars
GParsPool.withPool(numThreads) {
htmlFiles.eachParallel { file ->
try {
def content = file.text
// Check if Matomo code already exists
if (content.contains('<!-- Matomo -->') || content.contains('matomo.js')) {
synchronized(System.out) {
println "Skipping ${file.absolutePath} - Matomo code already present"
}
skippedCount.incrementAndGet()
return
}
def updatedContent
// Check if file has a closing </head> tag
// Insert Matomo code before </head>
if (content.contains('</head>')) {
updatedContent = content.replaceFirst('</head>', "${matomoCode}\n</head>")
} else if (content.contains('</HEAD>')) {
updatedContent = content.replaceFirst('</HEAD>', "${matomoCode}\n</HEAD>")
} else if (content.contains('<body class="center">')) {
updatedContent = content.replaceFirst('<body class="center">', "${matomoCode}\n<body class=\"center\">")
} else {
synchronized(System.out) {
println "Skipping ${file.absolutePath} - No </head> tag found"
}
skippedCount.incrementAndGet()
return
}
// Write the updated content back to the file
file.write(updatedContent)
synchronized(System.out) {
println "Updated ${file.absolutePath}"
}
updatedCount.incrementAndGet()
} catch (Exception e) {
synchronized(System.out) {
println "Error processing ${file.absolutePath}: ${e.message}"
}
}
}
}
println "\nProcessing complete!"
println "Files updated: ${updatedCount.get()}"
println "Files skipped: ${skippedCount.get()}"
println "Total files processed: ${htmlFiles.size()}"