-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
rehanvandermerwe
committed
Jun 13, 2023
1 parent
26b9ff6
commit a965685
Showing
8 changed files
with
171 additions
and
34 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import * as swaClient from "../"; | ||
|
||
(() => { | ||
/* Only specify the data tag serverless-website-analytics if the browser does not support `document.currentScript` | ||
* All modern browsers since 2015 do */ | ||
const me = document.currentScript || document.querySelector('script[serverless-website-analytics]'); | ||
if(!me) | ||
throw new Error("Could not find script tag with attribute 'serverless-website-analytics' on the script tag."); | ||
|
||
const site = me.getAttribute('site'); | ||
if(!site) | ||
throw new Error("Could not find attribute 'site' on the script tag."); | ||
|
||
/* The API URL to send the metrics to will 99% be the same as where the standalone script is loaded from, only use | ||
* the `api-url` if specified explicitly. */ | ||
let scriptOrigin = ""; | ||
try { | ||
//@ts-ignore | ||
scriptOrigin = new URL(me.src).origin; | ||
} catch (e) { | ||
console.error("Could not parse URL from script tag", e); | ||
} | ||
let apiUrl = me.getAttribute('api-url'); | ||
if(!scriptOrigin && !apiUrl) | ||
throw new Error("Could not auto-detect script origin, specify the 'api-url' attribute on the script tag."); | ||
if(!apiUrl) | ||
apiUrl = scriptOrigin; | ||
|
||
const routing = me.getAttribute('routing') | ||
if(routing && (routing !== "path" && routing !== "hash")) | ||
throw new Error("Attribute 'routing' must be either 'path' or 'hash'"); | ||
|
||
function getPath() { | ||
if(routing === "path") | ||
return window.location.pathname; | ||
else if(routing === "hash") | ||
return window.location.hash ? window.location.hash : "/"; | ||
else | ||
return window.location.pathname + (window.location.hash ? "#" + window.location.hash : ""); | ||
} | ||
|
||
//@ts-ignore | ||
window.swa = swaClient; | ||
swaClient.v1.analyticsPageInit({ | ||
inBrowser: true, | ||
site: site, | ||
apiUrl: apiUrl, | ||
// debug: true, | ||
}); | ||
|
||
let currentPage = location.href; | ||
setInterval(function() { | ||
if (currentPage != location.href) { | ||
currentPage = location.href; | ||
// console.log('New URL:', getPath()); | ||
swaClient.v1.analyticsPageChange(getPath()); | ||
} | ||
}, 500); | ||
|
||
swaClient.v1.analyticsPageChange(getPath()); | ||
|
||
})(); | ||
|
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>About</title> | ||
</head> | ||
<body> | ||
<h1>Test for Standalone - About</h1> | ||
|
||
<br> | ||
<br> | ||
|
||
<p>Click for the <a href="index.html">Home</a> page</p> | ||
|
||
|
||
<script src="<YOUR BACKEND ORIGIN>/cdn/client-script.js" site="<THE SITE YOU ARE TRACKING>"></script> | ||
<!--<script src="../../package/dist/cdn/client-script.js" serverless-website-analytics site="my-project.com" api-url="http://localhost:5000"></script>--> | ||
</body> | ||
</html> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>Home</title> | ||
</head> | ||
<body> | ||
<h1>Test for Standalone - Home</h1> | ||
|
||
<br> | ||
<br> | ||
|
||
<p>Click for the <a href="about.html">About</a> page</p> | ||
<p>Click for the <a href="index.html#test/something">Same with hash</a> page</p> | ||
|
||
<script src="<YOUR BACKEND ORIGIN>/cdn/client-script.js" site="<THE SITE YOU ARE TRACKING>"></script> | ||
<!--<script src="../../package/dist/cdn/client-script.js" serverless-website-analytics site="my-project.com" api-url="http://localhost:5000"></script>--> | ||
<!--<script src="../../package/dist/cdn/client-script.js" site="my-project.com"></script>--> | ||
</body> | ||
</html> |
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
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