Skip to content

Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.

Allow custom styles to be passed to embedded iframe (#1522)

Uku Taht7d37208d52c2
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 0170279fa1..d5ab5ff147 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -10,6 +10,7 @@ All notable changes to this project will be documented in this file.
- The `u` option can now be used in the `manual` extension to specify a URL when triggering events.
- Delete a site and all related data through the Sites API
- Subscribed users can see their Paddle invoices from the last 12 months under the user settings
+- Allow custom styles to be passed to embedded iframe plausible/analytics#1522
### Fixed
- UI fix where multi-line text in pills would not be underlined properly on small screens.
diff --git a/assets/css/app.css b/assets/css/app.css
index 3fc1397683..c492847be5 100644
--- a/assets/css/app.css
+++ b/assets/css/app.css
@@ -326,3 +326,7 @@ iframe[hidden] {
}
}
+
+/* This class is used for styling embedded dashboards. Do not remove. */
+/* stylelint-disable */
+.date-option-group { }
diff --git a/assets/js/dashboard/datepicker.js b/assets/js/dashboard/datepicker.js
index 5c046f9d18..d83f88667a 100644
--- a/assets/js/dashboard/datepicker.js
+++ b/assets/js/dashboard/datepicker.js
@@ -319,29 +319,25 @@ class DatePicker extends React.Component {
>
<div
className="rounded-md shadow-lg bg-white dark:bg-gray-800 ring-1 ring-black ring-opacity-5
- font-medium text-gray-800 dark:text-gray-200"
+ font-medium text-gray-800 dark:text-gray-200 date-options"
>
- <div className="py-1">
+ <div className="py-1 border-b border-gray-200 dark:border-gray-500 date-option-group">
{this.renderLink("day", "Today")}
{this.renderLink("realtime", "Realtime")}
</div>
- <div className="border-t border-gray-200 dark:border-gray-500"></div>
- <div className="py-1">
+ <div className="py-1 border-b border-gray-200 dark:border-gray-500 date-option-group">
{this.renderLink("7d", "Last 7 days")}
{this.renderLink("30d", "Last 30 days")}
</div>
- <div className="border-t border-gray-200 dark:border-gray-500"></div>
- <div className="py-1">
+ <div className="py-1 border-b border-gray-200 dark:border-gray-500 date-option-group">
{ this.renderLink('month', 'Month to Date') }
{ this.renderLink('month', 'Last month', {date: lastMonth(this.props.site)}) }
</div>
- <div className="border-t border-gray-200 dark:border-gray-500"></div>
- <div className="py-1">
+ <div className="py-1 border-b border-gray-200 dark:border-gray-500 date-option-group">
{this.renderLink("6mo", "Last 6 months")}
{this.renderLink("12mo", "Last 12 months")}
</div>
- <div className="border-t border-gray-200 dark:border-gray-500"></div>
- <div className="py-1">
+ <div className="py-1 date-option-group">
<span
onClick={() => this.setState({mode: 'calendar'}, this.openCalendar)}
onKeyPress={() => this.setState({mode: 'calendar'}, this.openCalendar)}
diff --git a/assets/js/embed.content.js b/assets/js/embed.content.js
index 7f22b3e898..32e50f7c6c 100644
--- a/assets/js/embed.content.js
+++ b/assets/js/embed.content.js
@@ -1 +1,16 @@
import 'iframe-resizer/js/iframeResizer.contentWindow'
+
+window.iFrameResizer = {
+ onMessage: function(msg) {
+ if (msg.type === 'load-custom-styles') {
+ addCustomStyles(msg.opts)
+ }
+ }
+}
+
+function addCustomStyles(opts) {
+ var style = document.createElement('style');
+ style.innerHTML = opts.styles
+
+ document.head.appendChild(style);
+}
diff --git a/assets/js/embed.host.js b/assets/js/embed.host.js
index 1d366566de..37d493b53f 100644
--- a/assets/js/embed.host.js
+++ b/assets/js/embed.host.js
@@ -1,3 +1,20 @@
import iframeResize from 'iframe-resizer/js/iframeResizer'
-iframeResize({heightCalculationMethod: 'taggedElement'}, '[plausible-embed]')
+var iframes = iframeResize({
+ heightCalculationMethod: 'taggedElement',
+ onInit: onInit
+}, '[plausible-embed]')
+
+function onInit() {
+ var iframe = iframes[0]
+ var styles = iframe.getAttribute('styles')
+
+ if (styles) {
+ iframe.iFrameResizer.sendMessage({
+ type: 'load-custom-styles',
+ opts: {
+ styles: styles
+ }
+ })
+ }
+}