Skip to content

Commit

Permalink
Allow deco analytics to use a defined domain (#267)
Browse files Browse the repository at this point in the history
  • Loading branch information
matheusgr authored Dec 29, 2023
1 parent 530619e commit 165515f
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
4 changes: 3 additions & 1 deletion analytics/components/DecoAnalytics.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export interface Props {
* @description paths to be excluded.
*/
exclude?: string;
domain?: string;
}

declare global {
Expand Down Expand Up @@ -78,7 +79,7 @@ const snippet = () => {
});
};

function Component({ exclude }: Props) {
function Component({ exclude, domain }: Props) {
return (
<Head>
<link rel="dns-prefetch" href="https://plausible.io/api/event" />
Expand All @@ -89,6 +90,7 @@ function Component({ exclude }: Props) {
/>
<script
defer
data-domain={domain}
data-exclude={`${"/proxy" + (exclude ? "," + exclude : "")}`}
data-api="https://plausible.io/api/event"
src="https://plausible.io/js/script.manual.js"
Expand Down
5 changes: 3 additions & 2 deletions analytics/loaders/DecoAnalyticsScript.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { AppContext } from "../mod.ts";
import { Script } from "../../website/types.ts";
import { exclusionPropsAndHashScript } from "../scripts/plausible_scripts.ts";
import { defaultExclusionPropsAndHashScript, exclusionPropsAndHashScript } from "../scripts/plausible_scripts.ts";
import { getFlagsFromCookies } from "../../utils/cookie.ts";

export type Props = {
defer?: boolean;
domain?: string;
};

const loader = (
Expand All @@ -28,7 +29,7 @@ const loader = (
) => (`event-${flagName}="${flagActive}"`)).join(
" ",
)
} data-api="https://plausible.io/api/event">${exclusionPropsAndHashScript}</script>`;
} ${props.domain ? "data-domain=" + props.domain : ""} data-api="https://plausible.io/api/event">${props.domain ? defaultExclusionPropsAndHashScript : exclusionPropsAndHashScript}</script>`;
return dnsPrefetchLink + preconnectLink + plausibleScript;
};
return ({ src: transformReq });
Expand Down
6 changes: 5 additions & 1 deletion analytics/scripts/plausible_scripts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@ export const localAndExclusionAndHashScript =
'!function(){"use strict";var c=window.location,o=window.document,u=o.currentScript,s=u.getAttribute("data-api")||new URL(u.src).origin+"/api/event";function p(e,t){e&&console.warn("Ignoring Event: "+e),t&&t.callback&&t.callback()}function e(e,t){try{if("true"===window.localStorage.plausible_ignore)return p("localStorage flag",t)}catch(e){}var i=u&&u.getAttribute("data-include"),n=u&&u.getAttribute("data-exclude");if("pageview"===e){i=!i||i.split(",").some(a),n=n&&n.split(",").some(a);if(!i||n)return p("exclusion rule",t)}function a(e){var t=c.pathname;return(t+=c.hash).match(new RegExp("^"+e.trim().replace(/\\*\\*/g,".*").replace(/([^\\.])\\*/g,"$1[^\\\\s/]*")+"/?$"))}var i={},n=(i.n=e,i.u=c.href,i.d=((w,d)=>{const h=w.location.hostname;return h.replace(/^www./,"")})(window,document),i.r=o.referrer||null,t&&t.meta&&(i.m=JSON.stringify(t.meta)),t&&t.props&&(i.p=t.props),u.getAttributeNames().filter(function(e){return"event-"===e.substring(0,6)})),r=i.p||{},l=(n.forEach(function(e){var t=e.replace("event-",""),e=u.getAttribute(e);r[t]=r[t]||e}),i.p=r,i.h=1,new XMLHttpRequest);l.open("POST",s,!0),l.setRequestHeader("Content-Type","text/plain"),l.send(JSON.stringify(i)),l.onreadystatechange=function(){4===l.readyState&&t&&t.callback&&t.callback()}}var t=window.plausible&&window.plausible.q||[];window.plausible=e;for(var i,n=0;n<t.length;n++)e.apply(this,t[n]);function a(){i=c.pathname,e("pageview")}window.addEventListener("hashchange",a),"prerender"===o.visibilityState?o.addEventListener("visibilitychange",function(){i||"visible"!==o.visibilityState||a()}):a()}();';

export const exclusionPropsAndHashScript =
'!function(){"use strict";var l=window.location,c=window.document,s=c.currentScript,u=s.getAttribute("data-api")||new URL(s.src).origin+"/api/event";function p(e,t){e&&console.warn("Ignoring Event: "+e),t&&t.callback&&t.callback()}function e(e,t){if(/^localhost$|^127(\\.[0-9]+){0,2}\\.[0-9]+$|^\\[::1?\\]$/.test(l.hostname)||"file:"===l.protocol)return p("localhost",t);if(window._phantom||window.__nightmare||window.navigator.webdriver||window.Cypress)return p(null,t);try{if("true"===window.localStorage.plausible_ignore)return p("localStorage flag",t)}catch(e){}var n=s&&s.getAttribute("data-include"),i=s&&s.getAttribute("data-exclude");if("pageview"===e){n=!n||n.split(",").some(a),i=i&&i.split(",").some(a);if(!n||i)return p("exclusion rule",t)}function a(e){var t=l.pathname;return(t+=l.hash).match(new RegExp("^"+e.trim().replace(/\\*\\*/g,".*").replace(/([^\\.])\\*/g,"$1[^\\\\s/]*")+"/?$"))}var n={},i=(n.n=e,n.u=l.href,n.d=s.getAttribute("data-domain"),n.r=c.referrer||null,t&&t.meta&&(n.m=JSON.stringify(t.meta)),t&&t.props&&(n.p=t.props),s.getAttributeNames().filter(function(e){return"event-"===e.substring(0,6)})),r=n.p||{},o=(i.forEach(function(e){var t=e.replace("event-",""),e=s.getAttribute(e);r[t]=r[t]||e}),n.p=r,n.h=1,new XMLHttpRequest);o.open("POST",u,!0),o.setRequestHeader("Content-Type","text/plain"),o.send(JSON.stringify(n)),o.onreadystatechange=function(){4===o.readyState&&t&&t.callback&&t.callback()}}var t=window.plausible&&window.plausible.q||[];window.plausible=e;for(var n,i=0;i<t.length;i++)e.apply(this,t[i]);function a(){n=l.pathname,e("pageview")}window.addEventListener("hashchange",a),"prerender"===c.visibilityState?c.addEventListener("visibilitychange",function(){n||"visible"!==c.visibilityState||a()}):a()}();';
'!function(){"use strict";var l=window.location,c=window.document,s=c.currentScript,u=s.getAttribute("data-api")||new URL(s.src).origin+"/api/event";function p(e,t){e&&console.warn("Ignoring Event: "+e),t&&t.callback&&t.callback()}function e(e,t){if(/^localhost$|^127(\\.[0-9]+){0,2}\\.[0-9]+$|^\\[::1?\\]$/.test(l.hostname)||"file:"===l.protocol)return p("localhost",t);if(window._phantom||window.__nightmare||window.navigator.webdriver||window.Cypress)return p(null,t);try{if("true"===window.localStorage.plausible_ignore)return p("localStorage flag",t)}catch(e){}var n=s&&s.getAttribute("data-include"),i=s&&s.getAttribute("data-exclude");if("pageview"===e){n=!n||n.split(",").some(a),i=i&&i.split(",").some(a);if(!n||i)return p("exclusion rule",t)}function a(e){var t=l.pathname;return(t+=l.hash).match(new RegExp("^"+e.trim().replace(/\\*\\*/g,".*").replace(/([^\\.])\\*/g,"$1[^\\\\s/]*")+"/?$"))}var n={},i=(n.n=e,n.u=l.href,n.d=((w,d)=>{const h=w.location.hostname;return h.replace(/^www./,"")})(window,document),n.r=c.referrer||null,t&&t.meta&&(n.m=JSON.stringify(t.meta)),t&&t.props&&(n.p=t.props),s.getAttributeNames().filter(function(e){return"event-"===e.substring(0,6)})),r=n.p||{},o=(i.forEach(function(e){var t=e.replace("event-",""),e=s.getAttribute(e);r[t]=r[t]||e}),n.p=r,n.h=1,new XMLHttpRequest);o.open("POST",u,!0),o.setRequestHeader("Content-Type","text/plain"),o.send(JSON.stringify(n)),o.onreadystatechange=function(){4===o.readyState&&t&&t.callback&&t.callback()}}var t=window.plausible&&window.plausible.q||[];window.plausible=e;for(var n,i=0;i<t.length;i++)e.apply(this,t[i]);function a(){n=l.pathname,e("pageview")}window.addEventListener("hashchange",a),"prerender"===c.visibilityState?c.addEventListener("visibilitychange",function(){n||"visible"!==c.visibilityState||a()}):a()}();';

// normal script to be used when data-domain is defined.
export const defaultExclusionPropsAndHashScript =
'!function(){"use strict";var o=window.location,l=window.document,c=l.currentScript,s=c.getAttribute("data-api")||new URL(c.src).origin+"/api/event";function u(e,t){e&&console.warn("Ignoring Event: "+e),t&&t.callback&&t.callback()}function e(e,t){if(/^localhost$|^127(\\.[0-9]+){0,2}\\.[0-9]+$|^\\[::1?\\]$/.test(o.hostname)||"file:"===o.protocol)return u("localhost",t);if(window._phantom||window.__nightmare||window.navigator.webdriver||window.Cypress)return u(null,t);try{if("true"===window.localStorage.plausible_ignore)return u("localStorage flag",t)}catch(e){}var i=c&&c.getAttribute("data-include"),n=c&&c.getAttribute("data-exclude");if("pageview"===e){i=!i||i.split(",").some(a),n=n&&n.split(",").some(a);if(!i||n)return u("exclusion rule",t)}function a(e){var t=o.pathname;return(t+=o.hash).match(new RegExp("^"+e.trim().replace(/\\*\\*/g,".*").replace(/([^\\.])\\*/g,"$1[^\\\\s/]*")+"/?$"))}var i={},r=(i.n=e,i.u=o.href,i.d=c.getAttribute("data-domain"),i.r=l.referrer||null,t&&t.meta&&(i.m=JSON.stringify(t.meta)),t&&t.props&&(i.p=t.props),i.h=1,new XMLHttpRequest);r.open("POST",s,!0),r.setRequestHeader("Content-Type","text/plain"),r.send(JSON.stringify(i)),r.onreadystatechange=function(){4===r.readyState&&t&&t.callback&&t.callback()}}var t=window.plausible&&window.plausible.q||[];window.plausible=e;for(var i,n=0;n<t.length;n++)e.apply(this,t[n]);function a(){i=o.pathname,e("pageview")}window.addEventListener("hashchange",a),"prerender"===l.visibilityState?l.addEventListener("visibilitychange",function(){i||"visible"!==l.visibilityState||a()}):a()}();'

0 comments on commit 165515f

Please sign in to comment.