{"id":21762,"date":"2015-09-29T10:41:00","date_gmt":"2015-09-29T10:41:00","guid":{"rendered":"https:\/\/www.arimetrics.com\/blog\/extract-data-from-google-analytics-with-r"},"modified":"2026-05-13T16:38:36","modified_gmt":"2026-05-13T16:38:36","slug":"extract-data-from-google-analytics-with-r","status":"publish","type":"post","link":"https:\/\/www.arimetrics.com\/en\/blog\/extract-data-from-google-analytics-with-r","title":{"rendered":"Extract data from Google Analytics with R"},"content":{"rendered":"R is an object-oriented language intended for statistical analysis. <a href=\"https:\/\/www.arimetrics.com\/en\/digital-glossary\/r\">A,<\/a>in all likelihood, one of the most used programming languages by statisticians and is used in many fields, from biotechnology to web analytics.\n\nOne of the great advantages of R is its versatility. If you&#8217;re not a programmer, it&#8217;s a language that shouldn&#8217;t scare you, and, if you follow this little tutorial, you&#8217;ll be extracting <strong>data from Google Analytics with R<\/strong> in less than an hour.\n<h4>Table of Contents<\/h4>\nFor this we will first install R.\n<h2>1. Download and install R<\/h2>\nYou will find the latest version for your operating system (Linux, Mac or Windows) on the official website of the R project\n\n<a href=\"https:\/\/cran.r-project.org\/bin\/windows\/Rtools\/\" rel=\"alternate noopener\" target=\"_blank\">https:\/\/cran.r-project.org\/bin\/windows\/Rtools\/<\/a>\n\nNext we will install RStudio. RStudio is an integrated development environment (IDE) for R, which will allow us to operate with R in a more intuitive way. You can download its latest version on the official site:\n\n<a href=\"https:\/\/www.rstudio.com\/products\/rstudio\/download\/\" rel=\"alternate noopener\" target=\"_blank\">https:\/\/www.rstudio.com\/products\/rstudio\/download\/<\/a>\n\nOnce we have RStudio, we must install RTools:\n\n<a href=\"https:\/\/cran.r-project.org\/bin\/windows\/Rtools\/\" rel=\"alternate noopener\" target=\"_blank\">https:\/\/cran.r-project.org\/bin\/windows\/Rtools\/<\/a>\n\n2.   Using R with RStudio\n\nOnce we have installed the software we can start the RStudio program.\n\nIn the left console, after the symbol &gt; we will enter the following commands:\n\n<strong>1) We install and load the devtools package<\/strong>\n\ninstall.packages(&#8220;devtools&#8221;)\nlibrary(devtools)\n\n<strong>2) We install and load the rga package<\/strong>\n\nThis package will be the connector that allows us to start the extraction of data from <a href=\"https:\/\/www.arimetrics.com\/consultores-google-analytics\">Google Analytics<\/a>\n\ninstall_github(&#8220;rga&#8221;, &#8220;skardhamar&#8221;)\nlibrary(rga)\n\n<strong>3) Authentication in Google Analytics<\/strong>\n\nrga.open(instance=&#8221;ga&#8221;)\n\nThen a tab will open in our browser requesting access and we will be provided with a password that we will enter in the console.\n\n<strong>4) View ID selection<\/strong>\n\nWe need to define what the id of the view on which we are going to perform the query will be. We can get a list of all the views with the command:\n\nga$getProfiles()\n\nWe will obtain a list, from which we will choose the profile we want (for example: 12345678), and we will save it in a variable with the following command:\n\nid &lt; &#8211; &#8220;12345678&#8221;\n\n<strong>5) Data query to Google Analytics<\/strong>\n\nFor the query we will use the command ga$getData.\n\nLet&#8217;s look at a query as an example:\n\nga$getData(id,\nstart.date=&#8221;2015-01-01&#8243;,\nend.date=&#8221;2015-03-31&#8243;,\nmetrics=&#8221;ga:sessions,ga:bounceRate, ga:percentNewSessions,ga:avgSessionDuration&#8221;,\ndimensions=&#8221;ga:yearMonth&#8221;,\nfilters=&#8221;ga:source==google&#8221;,\nsegment=&#8221;gaid::-5&#8243;)\n\nThis query will return the monthly data of sessions, bounce rate, % of new sessions and average duration of the session of the first quarter of 2005, for organic searches of the Google search engine.\n\nParameters that have been used in this query:\n<ul>\n \t<li>id: we have defined it as the view number<\/li>\n \t<li>start.date: The start date of the query. It has to go in YYYY-MM-DD format (year-month-day)<\/li>\n \t<li>end.date: End date of the query. It has to go in YYYY-MM-DD format (year-month-day)<\/li>\n \t<li>metrics: metrics<\/li>\n \t<li>dimensions: dimensions<\/li>\n \t<li>filters: filters<\/li>\n \t<li>segment: segment<\/li>\n<\/ul>\nIf you have any questions about which metrics or dimensions to use, you can always check <a href=\"https:\/\/developers.google.com\/analytics\/devguides\/reporting\/core\/dimsmets\" rel=\"alternate noopener\" target=\"_blank\">in developers.google.com\/analytics\/devguides\/reporting\/core\/dimsmets<\/a>\n\nAs you can see, this is just the beginning, but it is clarifying enough to see that you do not have to be a programmer to use R, and that anyone, with a little patience, can learn to use this useful language to perform advanced statistics.\n\n\n<div class=\"geo-faq-block\">\n  <h3 class=\"geo-faq-title\">Frequently asked questions<\/h3>\n  <details class=\"geo-faq-item\">\n    <summary>How can data be extracted from Google Analytics with R?<\/summary>\n    <p>To extract Google Analytics data with R, use a compatible library, authenticate the account, select the property, metrics, dimensions and dates, and save the results for later analysis.<\/p>\n  \n\n<div class=\"geo-answer-block\"><p><strong>Quick answer:<\/strong> Extract data from Google Analytics with R should be approached as a practical decision framework: clarify the objective, validate reliable data, prioritize actions with measurable impact and review results regularly. The goal is to connect each recommendation with business context, audience needs and continuous improvement.<\/p>\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Area<\/th><th>What to review<\/th><th>Useful indicator<\/th><\/tr><\/thead><tbody><tr><td>Objective<\/td><td>Define the business goal, audience and expected outcome before applying any tactic.<\/td><td>Qualified traffic, leads, sales or efficiency improvement.<\/td><\/tr><tr><td>Data and measurement<\/td><td>Check that the information used to decide is reliable, comparable and aligned with the channel.<\/td><td>Conversion rate, attribution quality and trend evolution.<\/td><\/tr><tr><td>Optimization<\/td><td>Prioritize actions by impact, effort and learning potential instead of applying isolated changes.<\/td><td>Improvement after implementation and next action identified.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n<\/div>\n\n<\/details>\n  <details class=\"geo-faq-item\">\n    <summary>Why connect Google Analytics with R?<\/summary>\n    <p>It helps automate reports, combine data with other sources, create reproducible analyses, apply statistical models and generate custom visualizations beyond standard dashboards.<\/p>\n  <\/details>\n  <details class=\"geo-faq-item\">\n    <summary>What knowledge is needed to use R with Analytics?<\/summary>\n    <p>Basic R, APIs, Analytics metrics and dimensions, authentication, data cleaning and table structure are useful. It is also important to understand the analysis objective.<\/p>\n  <\/details>\n  <details class=\"geo-faq-item\">\n    <summary>What mistakes should be avoided when extracting Analytics data with R?<\/summary>\n    <p>Avoid requesting incompatible metrics, failing to document queries, ignoring sampling or API limits, mixing properties and not validating that data matches the Analytics configuration.<\/p>\n  <\/details>\n<\/div>\n\n\n\n<script type=\"application\/ld+json\">\n{\n  \"@context\": \"https:\/\/schema.org\",\n  \"@type\": \"FAQPage\",\n  \"@id\": \"https:\/\/www.arimetrics.com\/en\/blog\/extract-data-from-google-analytics-with-r\/#faq\",\n  \"inLanguage\": \"en-US\",\n  \"mainEntity\": [\n    {\n      \"@type\": \"Question\",\n      \"name\": \"How can data be extracted from Google Analytics with R?\",\n      \"acceptedAnswer\": {\n        \"@type\": \"Answer\",\n        \"text\": \"To extract Google Analytics data with R, use a compatible library, authenticate the account, select the property, metrics, dimensions and dates, and save the results for later analysis.\"\n      }\n    },\n    {\n      \"@type\": \"Question\",\n      \"name\": \"Why connect Google Analytics with R?\",\n      \"acceptedAnswer\": {\n        \"@type\": \"Answer\",\n        \"text\": \"It helps automate reports, combine data with other sources, create reproducible analyses, apply statistical models and generate custom visualizations beyond standard dashboards.\"\n      }\n    },\n    {\n      \"@type\": \"Question\",\n      \"name\": \"What knowledge is needed to use R with Analytics?\",\n      \"acceptedAnswer\": {\n        \"@type\": \"Answer\",\n        \"text\": \"Basic R, APIs, Analytics metrics and dimensions, authentication, data cleaning and table structure are useful. It is also important to understand the analysis objective.\"\n      }\n    },\n    {\n      \"@type\": \"Question\",\n      \"name\": \"What mistakes should be avoided when extracting Analytics data with R?\",\n      \"acceptedAnswer\": {\n        \"@type\": \"Answer\",\n        \"text\": \"Avoid requesting incompatible metrics, failing to document queries, ignoring sampling or API limits, mixing properties and not validating that data matches the Analytics configuration.\"\n      }\n    }\n  ]\n}\n<\/script>\n\n","protected":false},"excerpt":{"rendered":"<p>R is an object-oriented language intended for statistical analysis. A,in all likelihood, one of the most used programming languages by statisticians and is used in many fields, from biotechnology to web analytics. One of the great advantages of R is its versatility. If you&#8217;re not a programmer, it&#8217;s a language that shouldn&#8217;t scare you, and, [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":24658,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[291],"tags":[],"class_list":["post-21762","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-web-analytics"],"_links":{"self":[{"href":"https:\/\/www.arimetrics.com\/en\/wp-json\/wp\/v2\/posts\/21762","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.arimetrics.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.arimetrics.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.arimetrics.com\/en\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/www.arimetrics.com\/en\/wp-json\/wp\/v2\/comments?post=21762"}],"version-history":[{"count":2,"href":"https:\/\/www.arimetrics.com\/en\/wp-json\/wp\/v2\/posts\/21762\/revisions"}],"predecessor-version":[{"id":71593,"href":"https:\/\/www.arimetrics.com\/en\/wp-json\/wp\/v2\/posts\/21762\/revisions\/71593"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.arimetrics.com\/en\/wp-json\/wp\/v2\/media\/24658"}],"wp:attachment":[{"href":"https:\/\/www.arimetrics.com\/en\/wp-json\/wp\/v2\/media?parent=21762"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.arimetrics.com\/en\/wp-json\/wp\/v2\/categories?post=21762"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.arimetrics.com\/en\/wp-json\/wp\/v2\/tags?post=21762"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}