[英]Has anyone had success using Matomo for Shiny usage tracking with a shinydashboard app?
I have been trying to get Matomo to work with a shiny app that uses shinydashboard and particularly dashboardPage but cannot get it to work.我一直在尝试让 Matomo 使用一个闪亮的应用程序,该应用程序使用了 Shinydashboard,尤其是dashboardPage,但无法让它工作。 has anyone had success doing this?
有没有人成功做到这一点?
I have tried using Sean Lopp's code ( https://shiny.rstudio.com/articles/usage-metrics.html ).我曾尝试使用 Sean Lopp 的代码 ( https://shiny.rstudio.com/articles/usage-metrics.html )。 This works with a fluidPage but not with the dashboardPage.
这适用于fluidPage,但不适用于dashboardPage。
If, instead of using dashboardPage as in the code below I use fluidPage I get the result I want and can see the tracker working.如果,而不是像下面的代码那样使用dashboardPage,我使用fluidPage,我会得到我想要的结果,并且可以看到跟踪器正在工作。
When using this code I get the following error:使用此代码时,我收到以下错误:
Error in tagAssert(header, type = "header", class = "main-header") :
Expected tag to be of type header
I have tried inserting the tags$head(HTML())) chunk in the dashboardHeader() and get the same error.我尝试在dashboardHeader() 中插入tags$head(HTML())) 块并得到相同的错误。 If I place it immediately after the dashboardHeader I get:
如果我立即将它放在 dashboardHeader 之后,我会得到:
Error in tagAssert(sidebar, type = "aside", class = "main-sidebar") :
Expected tag to be of type aside
This is an example of the code with dashboardPage.这是带有dashboardPage的代码示例。 The identical code but with dashboardPage replaced with fluidPage works.
相同的代码,但dashboardPage 替换为fluidPage 工作。
I would be grateful for any suggestions that anyone might have.如果有人提出任何建议,我将不胜感激。
Thanks谢谢
library(shiny)
library(shinydashboard)
ui <- dashboardPage(
tags$head(HTML(
"<script type='text/javascript'>
var _paq = _paq || [];
/* tracker methods like 'setCustomDimension' should be called before 'trackPageView' */
_paq.push(['setDocumentTitle', document.domain + '/' + document.title]);
_paq.push(['setCookieDomain', '#########']);
_paq.push(['trackPageView']);
_paq.push(['enableLinkTracking']);
(function() {
var u='###############################';
_paq.push(['setTrackerUrl', u+'piwik.php']);
_paq.push(['setSiteId', '9']);
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
g.type='text/javascript'; g.async=true; g.defer=true; g.src=u+'piwik.js'; s.parentNode.insertBefore(g,s);
})();
</script>"
)),
dashboardHeader(title = "THIS IS A HEADER"),
dashboardSidebar(),
dashboardBody(),
title = "Dashboard example"
)
# Define server logic required to draw a histogram
server <- function(input, output) {
}
# Run the application
shinyApp(ui = ui, server = server)
You need to put it in your server.R file.你需要把它放在你的 server.R 文件中。 This is what I use:
这是我使用的:
output$renderPiwikHead <- renderUI({
# don't include stats if just running local development
if ((session$clientData[["url_hostname"]] != "127.0.0.1")
&& (exists("PIWIK_SITE_ID") && !is.null(PIWIK_SITE_ID))) {
# https://shiny.rstudio.com/articles/usage-metrics.html
# https://shiny.rstudio.com/articles/packaging-javascript.html
# Render the Piwik UX stats tracking at the end of <head>
tags$head(
HTML(paste0(
"<!-- Piwik -->
<script type='text/javascript'>
var _paq = _paq || [];
_paq.push(['setUserId', '", user_name, "']);
/* tracker methods like 'setCustomDimension' should be called before 'trackPageView' */
_paq.push(['trackPageView']);
_paq.push(['enableLinkTracking']);
(function() {
var u='https://piwik.sandia.gov/';
_paq.push(['setTrackerUrl', u+'piwik.php']);
_paq.push(['setSiteId', '", PIWIK_SITE_ID, "']);
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
g.type='text/javascript'; g.async=true; g.defer=true;
g.src=u+'piwik.js'; s.parentNode.insertBefore(g,s);
})();
</script>
<!-- End Piwik Code -->")
)
)
}
}) })
You do not have to add it in the server.您不必将其添加到服务器中。 You simply put it in dashboardBody() In your case:
您只需将它放在dashboardBody() 中:
library(shiny)
library(shinydashboard)
ui <- dashboardPage(
dashboardHeader(title = "THIS IS A HEADER"),
dashboardSidebar(),
dashboardBody(
tags$head(HTML(
"<script type='text/javascript'>
var _paq = _paq || [];
/* tracker methods like 'setCustomDimension' should be called before 'trackPageView' */
_paq.push(['setDocumentTitle', document.domain + '/' + document.title]);
_paq.push(['setCookieDomain', '#########']);
_paq.push(['trackPageView']);
_paq.push(['enableLinkTracking']);
(function() {
var u='###############################';
_paq.push(['setTrackerUrl', u+'piwik.php']);
_paq.push(['setSiteId', '9']);
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
g.type='text/javascript'; g.async=true; g.defer=true; g.src=u+'piwik.js'; s.parentNode.insertBefore(g,s);
})();
</script>"
))
),
title = "Dashboard example"
)
# Define server logic required to draw a histogram
server <- function(input, output) {
}
# Run the application
shinyApp(ui = ui, server = server)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.