如何在RMarkdown生成的GitHub页面中包含Google Analytics(分析)? [英] How to include Google Analytics in an RMarkdown generated GitHub Page?

查看:81
本文介绍了如何在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 Pages上的网页.要创建站点,我运行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?

推荐答案

我刚刚想出了如何在将Google Analytics(分析)添加到我在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标头以包含包括:& 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中的<\head>结束标记.上面的方法可以解决这个问题.

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代码的正文中,则可以通过将ga代码粘贴在html_preserve命令之间来将其包含在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(分析)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆