本文介绍了如何在 RMarkdown 生成的 GitHub 页面中包含 Google Analytics?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在使用 R Markdown 生成的 GitHub Pages 网页中始终包含 Google Analytics 跟踪代码?

How can I always include the Google Analytics tacking code on my GitHub Pages webpage generated with R Markdown?

我目前正在使用 RMarkdown 创建一个托管在 GitHub 页面上的网页.为了创建站点,我运行 rmarkdown::render_site().此命令将编织所有 R Markdown 文档以创建相关的 .html 文件.

I am currently creating a webpage hosted on GitHub Pages using RMarkdown. To create site, I run rmarkdown::render_site(). This command will knit all R Markdown documents to create the relevant .html files.

但是,我想在我的网站上加入 Google Analytics.为此,我需要通过将 JavaScript 代码粘贴到我的主 index.html 文件中,将大学分析跟踪代码包含到我的媒体资源中.我可以通过直接编辑 index.html 文件轻松地做到这一点.但是,每次我运行 rmarkdown::render_site() 来渲染站点时,它都会重新编排 index.Rmd 文件,从而覆盖 index.html 文件,包括我之前直接输入的 Google Analytics 代码.

However, I want to include Google Analytics on my site. To do this, I need to include a University Analytics tracking code to my property by pasting the JavaScript code in my main index.html file. I can easily do this by editing the index.html file directly. However, every time I run rmarkdown::render_site(), to render the site, it re-knits the index.Rmd file, and thus overwrites the index.html file, including the Google Analytics code I previously entered directly.

如何解决此问题并始终在页面中包含我的 Google Analytics(分析)代码?

How can I fix this problem and always have my Google Analytics code contained in the page?

推荐答案

当我尝试将谷歌分析添加到我自己在 Github Pages 上的 RMD 站点时,我刚刚想出了如何完成这项工作.

I just figured out how to get this done while trying to add google analytics to my own RMD site on Github Pages.

第 1 步:创建一个包含 Google Analytics 脚本的 .html 文件,并将其保存在您网站的工作目录中.(简单地说,创建新的文本文件,粘贴脚本,保存filename.html".

Step 1: Create a .html file containing the Google Analytics Script, and save it in the working directory for your site. (Simply, create new text file, paste script, save "filename.html".

第 2 步: 调整 rmd 文件的 yaml 标头以包含 includes: &in_header: 参数,并引用包含跟踪代码的 .html 文件

Step 2: Adjust the yaml header of your rmd file to contain the includes: & in_header: arguments, and reference the .html file containing the tracking code

---
title: ""
output:
  html_document:
    includes:
       in_header: GA_Script.html
---

第 3 步:为与您的网站相关的每个 rmd 文件调整 yaml 标头,以便每个页面都向 Google Analytics(分析)报告.

Step 3: Adjust yaml headers for each rmd file related to your site so that each page reports back to Google Analytics.

建议放置 Google Analytics 跟踪代码在站点 html 中的 结束标记之前.上面的方法将完成.

It is recommended that the Google Analytics tracking code be placed before the closing <head> tag in the sites html. The method above will get that done.

如果出于某种原因您想将其包含在 html 代码的正文中,您可以通过将其粘贴到 html_preserve 命令之间来将 GA 代码包含在 rmd 文件的正文中:

If for some reason you want to include it in the body of the html code, you could just include the GA code in the body of the rmd file by pasting it in between an html_preserve command:

<!--html_preserve-->

Google Analytics Code Here

<!--/html_preserve-->

这篇关于如何在 RMarkdown 生成的 GitHub 页面中包含 Google Analytics?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 07:48