Start learning with our library of video tutorials taught by experts. Get started
Viewers: in countries Watching now:
Get a new Google Analytics tip every other week from online marketing expert Corey Koberg. Most users unlock just a fraction of the power that Google Analytics offers, so in this course Corey exposes tips and tricks to unlock insights into one of the most sophisticated tools in the marketer or site owner's arsenal. He offers peeks into the latest power features, advice for deeply mining your digital data, and actions you can take to optimize your site for both traffic and conversions. Corey answers common questions about online marketing and web analytics, including installation, tag management, reporting, custom variables/dimensions, attribution modeling, segmentation, multichannel funnels, data accuracy, visualizations, Universal Analytics, and more. What's more, Corey welcomes your questions and will shape future videos based on member requests, so send them to us at firstname.lastname@example.org.
One of the most useful things we can do with our Analytics data is to import that data into other systems. Scientists utilize the incredibly powerful open source R programming language to do advanced statistical modeling and visualizations. So pulling our Analytics data into R allows us to take advantage of that power and enhance our analysis. This is by no means an R programming course. So, for now, I want to show you how to connect the two sources, so that your Google Analytics data can be pulled into the R environment. First thing we want to do is download and install that R environment.
Simply go to r-project.org and follow these download links. Go to the US mirror here at Berkeley and download from this. 'Kay, we'll simply follow the prompts here and install this. Okay. Now this will give you the base environment that is technically all you need. People prefer a more robust editing environment, so we're going to download a free editor called RStudio as well. I highly recommend it and it's rstudio.com.
Then we click on Download RStudio and you want the free version here, Desktop. Okay. Now, we've got the base R tools now. And we're going to go ahead and open up RStudio first. Now the next thing we'll need to do is install a connector between Google Analytics and R. There's several ways to do this, but RGA is a very simple library that has some built-in functions that pull in Google Analytics data very easily. So to do this, we're going to first install Devtools via this console by typing in install.packages("devtools").
Okay. That's installed successfully there. And then, we can actually launch that library devtools. And now, we're going to pull down RGA itself by typing install_github("rga","skardhamar"). It's going to actually download that, install that, and then we're going to launch that library just like we did similarly there. RGA, and we're all set.
Okay. Now that we're connected. Let's run a quick query and pull in some data. The easiest way to do this is by starting with the Query Explorer first. Now, if you're not familiar with the Google Analytics Query Explorer, you can see my other tips called GA API in 5 Minutes. But for now, we'll go ahead and pull that up and assume that you have a working knowledge of that. Now we just find this by going to Google and typing in query explorer and the first thing that pops up there. Okay. Remember, we've got our Account, Property, and View here. And what that's really going to identify here is this number, this its profile ID that we want to grab.
So, remember, this one right here is a number we're going to need to pull in. It's one of the things that Query Explorer can help us find. And let's just run a really simple query to pull in some basic GA data. So, let's say for dimensions here let's grab a date. And for metrics, we want to grab users. And we can do a filter. So let's just filter this to only people in, take a region, so let's say here the US. And then I'm going to grab our date range. So, the 23rd through, lets say, the 25th Okay.
So we did that here and we've got our dimension of date. We've got these three dates, the 23rd, the 24th, and the number of users for each of those. And what we want to try and do is make sure that when we replicate this on the R side that we pull in this data to match exactly what we've got here. So, switching back to the R Studio environment here, what I'm going to do is pull up a script that's going to do exactly that. So, I'ma load a script here. And the very top, we've got a place where we're going to keep the start date, just like we had before the 23rd, and the end date. It's going to call for that library. It's going to load this RGA library. The second thing it's going to do here is it's going to authenticate.
And remember, we need to basically tell Google Analytics, through this script, that it should allows us to access that data. So the script itself has to authenticate to Google somehow. So I'll show you how we'll do that. Once it's done that the next thing that we're going to do is open an instance for that and we're going to pull down exactly what we just had there. We're going to set that IDS to exactly what we had over there in the query explorer. We're going to set it to be batched as true and a couple other things here. Set the start date. Set the end date. The metrics are exactly what we had there, GA users, GA date.
And we're going to sort it by date and we're going to filter it here, the country equals US. And then we're going to write that to a CSV file. And that's pretty much it. So, what we're trying to do here is replicate what we've done in the Query Explorer and pull that into our R environment. Okay. So let's go ahead and run this. I'm going to do Ctrl + A, Ctrl+Shift+Enter. What it's going to do is it's going to launch this browser window and it's going to ask me if I want to authenticate this. Our Google Analytics would like to access my data. Do I want to accept that or not as my current user. So if I click that I want to accept this, it's going to give me this code string, here that I can then go back and paste into our studio, down here.
And it's going to authenticate. Now, once it's done that, it goes and it runs the query and it pulls back the data. What you want to do is make sure that the data that we've got here in R is going to match what we had in the Query Explorer. So we see here, we've got 23rd, 24th, 25th, 720, 696, 637. So we've done it. Now, it's also written out the CSV file. So we can go ahead and see what that looks like. And it's going to be a CSV that has exactly that data in it. So we've successfully pulled the data into R and then exported that out to a CSV.
And one thing I want to point out here is sometimes you'll have authentication issues, for some reason. It tries to go on before it authenticates. If that happens what you want to do is come up here and just highlight to the part where it goes to the authentication. And then when you do your Ctrl+Shift+Enter, it's only going to launch up to that part. And and that will make it such that it can authenticate first, and you won't skip past that in there. So sometimes when it asks you to. Enter in that string that you've got copied there.
It's already gone past that in the script. So, to get past that you simply highlight only up to the authentication part and then go from there. After you've authenticated, you're going to be authenticated and it won't ask you to do it again. You can just run the scripts as you want once you've authenticated it's, it's, you don't need to do it every single time. So, now we've seen how you can connect the data from Google with the mathematical horsepower of R. We've obviously just scratched the surface here. And, although, I would love to hear the different ways that you are using these two. So, please let me know in a feedback or via Twitter. Also, if you have any particular analysis challenges or examples you'd like to Google Analytics in R, please let me know that as well.
Find answers to the most frequently asked questions about Google Analytics Tips.
Here are the FAQs that matched your search "":
Sorry, there are no matches for your search ""—to search again, type in another word or phrase and click search.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.