Skip to content

Commit 728f670

Browse files
committed
Improve auto events and add to deploy
1 parent 74dde85 commit 728f670

File tree

4 files changed

+61
-22
lines changed

4 files changed

+61
-22
lines changed

deploy.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ if [[ $REPLY =~ ^[Yy]$ ]]; then
4444
fi
4545

4646
echo "==> Deploying non SRI v$VERSION files to $SERVER_NAME"
47+
rsync --rsync-path="sudo rsync" "$SCRIPTS_LATEST_PATH/auto-events.js" "$REMOTE_PATH/auto-events.js"
48+
rsync --rsync-path="sudo rsync" "$SCRIPTS_LATEST_PATH/auto-events.js.map" "$REMOTE_PATH/auto-events.js.map"
4749
rsync --rsync-path="sudo rsync" "$SCRIPTS_LATEST_PATH/e.js" "$REMOTE_PATH/events.js"
4850
rsync --rsync-path="sudo rsync" "$SCRIPTS_LATEST_PATH/e.js.map" "$REMOTE_PATH/events.js.map"
4951
rsync --rsync-path="sudo rsync" "$SCRIPTS_LATEST_PATH/latest.dev.js" "$REMOTE_PATH/latest.dev.js"
@@ -60,6 +62,10 @@ if [[ $REPLY =~ ^[Yy]$ ]]; then
6062
echo "==> Copying SRI v$VERSION file to $SERVER_NAME"
6163
rsync --quiet --rsync-path="sudo rsync" "./dist/v$VERSION/custom/v$VERSION.js" "$REMOTE_PATH/v$VERSION/app.js"
6264
rsync --quiet --rsync-path="sudo rsync" "./dist/v$VERSION/custom/v$VERSION.js.map" "$REMOTE_PATH/v$VERSION/app.js.map"
65+
rsync --quiet --rsync-path="sudo rsync" "./dist/v$VERSION/custom/light.js" "$REMOTE_PATH/v$VERSION/light.js"
66+
rsync --quiet --rsync-path="sudo rsync" "./dist/v$VERSION/custom/light.js.map" "$REMOTE_PATH/v$VERSION/light.js.map"
67+
rsync --quiet --rsync-path="sudo rsync" "./dist/v$VERSION/custom/auto-events.js" "$REMOTE_PATH/v$VERSION/auto-events.js"
68+
rsync --quiet --rsync-path="sudo rsync" "./dist/v$VERSION/custom/auto-events.js.map" "$REMOTE_PATH/v$VERSION/auto-events.js.map"
6369

6470
echo -e "==> ${GREEN}Woop woop! Deployed to $SERVER_NAME!${RESET}"
6571
else

minify.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ const templates = [
120120
variables: {
121121
...DEFAULTS,
122122
minify: false,
123-
version: 2,
124-
scriptName: "cloudflare_2",
123+
version: VERSION,
124+
scriptName: `cloudflare_${VERSION}`,
125125
sri: false,
126126
baseUrl: "{{cloudFlareCustomDomain}}",
127127
overwriteOptions: {
@@ -199,15 +199,25 @@ const templates = [
199199
baseUrl: "{{nginxHost}}",
200200
},
201201
},
202+
{
203+
type: "js",
204+
input: `${__dirname}/src/auto-events.js`,
205+
output: `custom/auto-events.js`,
206+
variables: {
207+
version: VERSION,
208+
sri: true,
209+
minify: true,
210+
url: "docs.simpleanalytics.com/automated-events",
211+
},
212+
},
202213
{
203214
type: "js",
204215
input: `${__dirname}/src/auto-events.js`,
205216
output: `auto-events.js`,
206217
variables: {
218+
version: VERSION,
219+
sri: true,
207220
minify: true,
208-
version: 1,
209-
sri: false,
210-
script: "auto-events.js",
211221
url: "docs.simpleanalytics.com/automated-events",
212222
},
213223
},

src/auto-events.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
if (typeof window === "undefined") return;
44

55
var log = function (message, type) {
6-
var logger = type === "warn" ? console.warn : console.log;
7-
return logger("Simple Analytics auto events:", message);
6+
var logger = type === "warn" ? console.warn : console.info;
7+
return logger && logger("Simple Analytics auto events:", message);
88
};
99

1010
var doc = window.document;
@@ -29,6 +29,8 @@
2929
})
3030
.filter(Boolean);
3131
else if (type === "array") return defaultValue;
32+
33+
return value || defaultValue;
3234
};
3335

3436
var collectTypes = setting("collect", "array", [
@@ -63,6 +65,8 @@
6365
downloadsFullUrl: fullUrls,
6466
};
6567

68+
var saGlobal = setting("saGlobal", "string", "sa_event");
69+
6670
// For minifying the script
6771
var optionsLink = options;
6872

@@ -80,7 +84,7 @@
8084
sent = true;
8185
};
8286

83-
if (window.sa_event) {
87+
if (window[saGlobal] && window[saGlobal + "_loaded"]) {
8488
var hostname = element.hostname;
8589
var pathname = element.pathname;
8690
var useTitle = false;
@@ -118,13 +122,13 @@
118122
"_" +
119123
event.replace(/[^a-z0-9]+/gi, "_").replace(/(^_+|_+$)/g, "");
120124

121-
sa_event(clean, callback);
125+
window[saGlobal](clean, callback);
122126

123127
log("collected " + clean);
124128

125129
return window.setTimeout(callback, 5000);
126130
} else {
127-
log("sa_event is not defined", "warn");
131+
log(saGlobal + " is not defined", "warn");
128132
return callback();
129133
}
130134
} catch (error) {

src/default.js

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
var pagehide = "pagehide";
5353
var platformText = "platform";
5454
var platformVersionText = "platformVersion";
55+
var docsUrl = "https://docs.simpleanalytics.com";
5556
var isBotAgent =
5657
/(bot|spider|crawl)/i.test(userAgent) && !/(cubot)/i.test(userAgent);
5758
/** if screen **/
@@ -270,10 +271,9 @@
270271
/** endif **/
271272

272273
// Customers can overwrite their hostname, here we check for that
273-
var definedHostname =
274-
overwriteOptions.hostname ||
275-
attr(scriptElement, "hostname") ||
276-
locationHostname;
274+
var overwrittenHostname =
275+
overwriteOptions.hostname || attr(scriptElement, "hostname");
276+
var definedHostname = overwrittenHostname || locationHostname;
277277

278278
/** if (or spa hash) **/
279279
// Some customers want to collect page views manually
@@ -363,20 +363,39 @@
363363
// Don't track when Do Not Track is set to true
364364
/** if ignorednt **/
365365
if (!collectDnt && doNotTrack in nav && nav[doNotTrack] == "1")
366-
return warn(notSending + "when " + doNotTrack + " is enabled");
366+
return warn(
367+
notSending +
368+
"when " +
369+
doNotTrack +
370+
" is enabled. See " +
371+
docsUrl +
372+
"/dnt"
373+
);
367374
/** else **/
368375
if (doNotTrack in nav && nav[doNotTrack] == "1")
369-
return warn(notSending + "when " + doNotTrack + " is enabled");
376+
return warn(
377+
notSending +
378+
"when " +
379+
doNotTrack +
380+
" is enabled. See " +
381+
docsUrl +
382+
"/dnt"
383+
);
370384
/** endif **/
371385

372-
/** unless testing **/
373-
// Don't track when localhost or when it's an IP address
386+
// Warn when sending from localhost and not having a hostname set
374387
if (
375-
locationHostname.indexOf(".") == -1 ||
376-
/^[0-9]+$/.test(locationHostname.replace(/\./g, ""))
388+
(locationHostname.indexOf(".") == -1 ||
389+
/^[0-9.:]+$/.test(locationHostname)) &&
390+
!overwrittenHostname
377391
)
378-
return warn(notSending + "from " + locationHostname);
379-
/** endunless **/
392+
warn(
393+
"Set a hostname when sending data from " +
394+
locationHostname +
395+
". See " +
396+
docsUrl +
397+
"/overwrite-domain-name"
398+
);
380399

381400
/////////////////////
382401
// SETUP INITIAL VARIABLES

0 commit comments

Comments
 (0)