Skip to content

Commit 9cef026

Browse files
author
Adriaan
authored
Merge pull request #20 from simpleanalytics/add-auto-events-script
Add auto events script and add page_id and session_id
2 parents f8c92c8 + 10fa144 commit 9cef026

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+256
-210
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

dist/latest/auto-events.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/latest/auto-events.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/latest/cloudflare.js

Lines changed: 76 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Simple Analytics - Privacy friendly analytics (docs.simpleanalytics.com/script; 2021-11-25; 49af; v2) */
1+
/* Simple Analytics - Privacy friendly analytics (docs.simpleanalytics.com/script; 2021-12-30; 26ba; v8) */
22
/* eslint-env browser */
33

44
(function (window, overwriteOptions, baseUrl, apiUrlPrefix, version, saGlobal) {
@@ -51,38 +51,11 @@
5151
var pagehide = "pagehide";
5252
var platformText = "platform";
5353
var platformVersionText = "platformVersion";
54+
var docsUrl = "https://docs.simpleanalytics.com";
5455
var isBotAgent =
5556
/(bot|spider|crawl)/i.test(userAgent) && !/(cubot)/i.test(userAgent);
5657
var screen = window.screen;
5758

58-
/////////////////////
59-
// PAYLOAD FOR BOTH PAGE VIEWS AND EVENTS
60-
//
61-
62-
var bot =
63-
nav.webdriver ||
64-
window.__nightmare ||
65-
"callPhantom" in window ||
66-
"_phantom" in window ||
67-
"phantom" in window ||
68-
isBotAgent;
69-
70-
71-
var payload = {
72-
version: version,
73-
ua: userAgent,
74-
};
75-
if (bot) payload.bot = true;
76-
77-
payload.sri = false;
78-
79-
// Use User-Agent Client Hints for better privacy
80-
// https://web.dev/user-agent-client-hints/
81-
if (uaData) {
82-
payload.mobile = uaData.mobile;
83-
payload.brands = stringify(uaData.brands);
84-
}
85-
8659
/////////////////////
8760
// HELPER FUNCTIONS
8861
//
@@ -186,7 +159,7 @@
186159

187160
// Send data via image
188161
var sendData = function (data, callback) {
189-
data = assign(payload, data);
162+
data = assign(payload, page, data);
190163

191164
var image = new Image();
192165
if (callback) {
@@ -281,10 +254,9 @@
281254
attr(scriptElement, "collect-dnt") == trueText;
282255

283256
// Customers can overwrite their hostname, here we check for that
284-
var definedHostname =
285-
overwriteOptions.hostname ||
286-
attr(scriptElement, "hostname") ||
287-
locationHostname;
257+
var overwrittenHostname =
258+
overwriteOptions.hostname || attr(scriptElement, "hostname");
259+
var definedHostname = overwrittenHostname || locationHostname;
288260

289261
// Some customers want to collect page views manually
290262
var autoCollect = !(
@@ -307,11 +279,41 @@
307279
? ignorePagesRaw.split(/, ?/)
308280
: [];
309281

282+
// Customers can ignore certain pages
283+
var ignorePagesRaw =
284+
overwriteOptions.ignorePages || attr(scriptElement, "ignore-pages");
285+
310286
/////////////////////
311-
// ADD HOSTNAME TO PAYLOAD
287+
// PAYLOAD FOR BOTH PAGE VIEWS AND EVENTS
312288
//
313289

314-
payload.hostname = definedHostname;
290+
var bot =
291+
nav.webdriver ||
292+
window.__nightmare ||
293+
"callPhantom" in window ||
294+
"_phantom" in window ||
295+
"phantom" in window ||
296+
isBotAgent;
297+
298+
var payload = {
299+
version: version,
300+
ua: userAgent,
301+
https: loc.protocol == https,
302+
timezone: timezone,
303+
hostname: definedHostname,
304+
page_id: uuid(),
305+
session_id: uuid(),
306+
};
307+
if (bot) payload.bot = true;
308+
309+
payload.sri = false;
310+
311+
// Use User-Agent Client Hints for better privacy
312+
// https://web.dev/user-agent-client-hints/
313+
if (uaData) {
314+
payload.mobile = uaData.mobile;
315+
payload.brands = stringify(uaData.brands);
316+
}
315317

316318
/////////////////////
317319
// ADD WARNINGS
@@ -327,21 +329,34 @@
327329

328330
// Don't track when Do Not Track is set to true
329331
if (!collectDnt && doNotTrack in nav && nav[doNotTrack] == "1")
330-
return warn(notSending + "when " + doNotTrack + " is enabled");
332+
return warn(
333+
notSending +
334+
"when " +
335+
doNotTrack +
336+
" is enabled. See " +
337+
docsUrl +
338+
"/dnt"
339+
);
331340

332-
// Don't track when localhost or when it's an IP address
341+
// Warn when sending from localhost and not having a hostname set
333342
if (
334-
locationHostname.indexOf(".") == -1 ||
335-
/^[0-9]+$/.test(locationHostname.replace(/\./g, ""))
343+
(locationHostname.indexOf(".") == -1 ||
344+
/^[0-9.:]+$/.test(locationHostname)) &&
345+
!overwrittenHostname
336346
)
337-
return warn(notSending + "from " + locationHostname);
347+
warn(
348+
"Set a hostname when sending data from " +
349+
locationHostname +
350+
". See " +
351+
docsUrl +
352+
"/overwrite-domain-name"
353+
);
338354

339355
/////////////////////
340356
// SETUP INITIAL VARIABLES
341357
//
342358

343359
var page = {};
344-
var lastPageId = uuid();
345360
var lastSendPath;
346361

347362
// We don't want to end up with sensitive data so we clean the referrer URL
@@ -372,7 +387,7 @@
372387
var sendBeaconText = "sendBeacon";
373388

374389
var sendOnLeave = function (id, push) {
375-
var append = { type: "append", original_id: push ? id : lastPageId };
390+
var append = { type: "append", original_id: push ? id : payload.page_id };
376391

377392
append[duration] = Math.round((now() - start - msHidden) / thousand);
378393
msHidden = 0;
@@ -381,6 +396,7 @@
381396
append.scrolled = Math.max(0, scrolled, position());
382397

383398
if (push || !(sendBeaconText in nav)) {
399+
// sendData will assign payload to request
384400
sendData(append);
385401
} else {
386402
nav[sendBeaconText](
@@ -468,23 +484,20 @@
468484

469485
// Send page view and append data to it
470486
var sendPageView = function (isPushState, deleteSourceInfo, sameSite) {
471-
if (isPushState) sendOnLeave("" + lastPageId, true);
472-
lastPageId = uuid();
473-
page.id = lastPageId;
487+
if (isPushState) sendOnLeave("" + payload.page_id, true);
488+
payload.page_id = uuid();
474489

475490
var currentPage = definedHostname + getPath();
476491

477492
sendData(
478493
assign(
479-
page,
480494
deleteSourceInfo
481495
? {
482496
referrer: sameSite ? referrer : null,
483497
}
484498
: source,
485499
{
486-
https: loc.protocol == https,
487-
timezone: timezone,
500+
id: payload.page_id,
488501
type: pageviewsText,
489502
}
490503
)
@@ -501,24 +514,20 @@
501514
if (!path || lastSendPath == path) return;
502515

503516
lastSendPath = path;
517+
page.path = path;
504518

505-
var data = {
506-
path: path,
507-
viewport_width:
508-
Math.max(documentElement[clientWidth] || 0, window.innerWidth || 0) ||
509-
null,
510-
viewport_height:
511-
Math.max(
512-
documentElement[clientHeight] || 0,
513-
window.innerHeight || 0
514-
) || null,
515-
};
519+
page.viewport_width =
520+
Math.max(documentElement[clientWidth] || 0, window.innerWidth || 0) ||
521+
null;
522+
page.viewport_height =
523+
Math.max(documentElement[clientHeight] || 0, window.innerHeight || 0) ||
524+
null;
516525

517-
if (nav[language]) data[language] = nav[language];
526+
if (nav[language]) page[language] = nav[language];
518527

519528
if (screen) {
520-
data.screen_width = screen.width;
521-
data.screen_height = screen.height;
529+
page.screen_width = screen.width;
530+
page.screen_height = screen.height;
522531
}
523532

524533
// If a user does refresh we need to delete the referrer because otherwise it count double
@@ -546,9 +555,7 @@
546555
: false;
547556

548557
// We set unique variable based on pushstate or back navigation, if no match we check the referrer
549-
data.unique = isPushState || userNavigated ? false : !sameSite;
550-
551-
page = data;
558+
page.unique = isPushState || userNavigated ? false : !sameSite;
552559

553560
var triggerSendPageView = function () {
554561
fetchedHighEntropyValues = true;
@@ -598,6 +605,7 @@
598605
event = new Event(type);
599606
} else {
600607
// Fix for IE
608+
// https://github.com/simpleanalytics/scripts/issues/8
601609
event = doc.createEvent("Event");
602610
event.initEvent(type, true, true);
603611
}
@@ -647,7 +655,6 @@
647655
// EVENTS
648656
//
649657

650-
var sessionId = uuid();
651658
var validTypes = ["string", "number"];
652659

653660
var sendEvent = function (event, callbackRaw) {
@@ -676,11 +683,9 @@
676683

677684
if (event) {
678685
sendData(
679-
assign(source, bot ? { bot: true } : {}, {
686+
assign(source, {
680687
type: "event",
681688
event: event,
682-
page_id: page.id,
683-
session_id: sessionId,
684689
}),
685690
callback
686691
);
@@ -718,6 +723,6 @@
718723
{"saGlobal":INSTALL_OPTIONS.sa_global,"mode":INSTALL_OPTIONS.hash_mode ? 'hash' : null,"collectDnt":INSTALL_OPTIONS.collect_dnt},
719724
INSTALL_OPTIONS.custom_domain || "queue.simpleanalyticscdn.com",
720725
"",
721-
"cloudflare_2",
726+
"cloudflare_8",
722727
"sa_event"
723728
);

0 commit comments

Comments
 (0)