Accessing data from different properties in google analytics - javascript

I have different properties in Google Analytics account. One for the main website and other for the android app. I am currently using Google Embed API in Javascript to access the data from the first property. Now I want to access the data from the second property as well. Is there any way to access the data from the other property in the same code.
I'm new to Google Analytics. It would be really helpful if you could also provide any reference material on how to do it.

There is no equivalent to the Embed API for Android, but there are Java client libraries for querying the various Google APIs that should do what you need.
This page lists all the client libraries for various languages and should be useful to you in general:
https://developers.google.com/analytics/devguides/reporting/core/v3/gdataLibraries

Thanks for the help. The solution was fairly simple. All I had to do was to change the ID in the Query to the value of the ID of the account I wanted to use and the request was sent corresponding account.

Related

How a list of all a users Google analytics views

I have many Analytics properties and accounts.
I am now working on JavaScript to get the reports from the Google Analytics API.
In order to make these requests I need the view idea for the account i wish to access so that i can load the related data from the backed.
The only way i have found to find these view ids is to use the Official GA Query Explorer every time to get the id by selecting Account and Property.
This is quite annoying. Because it involves human action.
Is there any way I can look up the id by the UA-xxxx string?
If you are already using the Google Analtyics API to get the reports why not use the Management APi to get a list of all the accounts the user has access to
Account summeries.list will give you that.
I am not a Javascript developer but the code should be something like this.
function queryAccounts() {
// Load the Google Analytics client library.
gapi.client.load('analytics', 'v3').then(function() {
// Get a list of all Google Analytics accounts for this user
gapi.client.analytics.management.accountSummaries.list().then(handleAccounts);
});
}
Code shamelessly ripped from Hello Analytics API: JavaScript quickstart for web applications and altered a bit using a wild guess.

Google Calendar API key embedded in JS?

I'm writing an app in JS that fetches Google Calendar events using Google Calendar API. I've already generated an API Key and Client ID - do I have to do something to try and 'secure' them? Their tutorial puts them directly in the JS file (https://developers.google.com/calendar/quickstart/js), but then I dig further into the dev docs (https://cloud.google.com/docs/authentication/api-keys#securing_an_api_key) and it says not to embed the API key into the code...but that could just be for paid API keys?
I'm writing this mini webapp for a class so I only need it to work for like 6 weeks. I'm hosting it via GitHub Pages, and it was the one that alerted me about the API Key when I pushed the code.
Bottom line - is it safe for me to embed the API key into the JS code that gets pushed to github and made publicly available (so I can have just a frontend and use GitHub Pages)? Or do I need to take some kind of preventative measures?
Thanks in advance!
Api key is only used for accessing public data. For example you could use it to access the Google Calendar public holiday calendars. That being said yes you should keep your api key secrete and not share it but google is aware that this is not possible with client side languages like JavaScript so i have never heard of anyone getting in trouble for leaking their api key by having it in a JavaScript application.
This warning is mainly means that you should not put it in a GitHub open source repository that anyone can download and then run. You must instruct people how to create their own.
That being said if you are trying to access private user data then you should be using Oauth2 to authenticate your users and this you can lock down to only your domain due to the need for a redirect uri. As you say you have created a client id i suspect that you have already done that. Apikey in the javascript code isnt strictly needed if you have added the client id for oauth2.

Show unique/returning user count on my website

I am working on the admin dashboard of my website and I was wondering if there is a way to display unique or returning user count.
Can I link/fetch my google analytics return value in any way possible.
This way the admin does not necessarily needs to go to Google Analytics, simply logon to the website and have the information readily available.
If you need historical data i think that the easiest way to do that is using the embed api
https://developers.google.com/analytics/devguides/reporting/embed/v1/
With that you can create easyly some chart to be embed on you html page, if yu can handle you can use the api.
Now,If you want to do it with the realtime report( the analytics module), it's not possible, because that metric is not available on that plataform

How to google maps api keys safe on website

I am using the google maps api on my website and would like to use my API key, but I'm confused about how to keep it safe. I know that I could just hardcode the actual key right into index.html
<!DOCTYPE html>
<html>
<head>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&key=HARDCODED_KEY"></script>
But then it's right there for anyone to see when they view the source code for the site. I also don't want it to show up in the DOM if someone's viewing the site with a debugging tool, like Firebug.
I'm assuming that there is a way to store it in a separate file (probably outside my html/ directory) that I can source. If anyone could provide an example or comment on whether this approach would be relatively secure, I'd really appreciate the help. I've found other posts on this topic, but none that apply for this (relatively simple) case.
Just set the referrers, as mentioned in the documentation:
To prevent other applications from using your key and consuming your
quota, you can limit the IP addresses that can use your API key to
send requests:
Visit the Google Developers Console and log in with your Google
account. Select the project that was created for you when you signed
up. The project name will start with Google Maps API for Work.In the
sidebar on the left, select Credentials. Find the key you're using
under the Public API Access heading, and click Edit allowed IPs. Enter
the IP addresses from which your key is to be accepted, one per line.
You may also enter a subnet using CIDR notation (e.g. 192.168.0.0/22).
Also you may come up with this question after you set the referral, I think you'll find it useful.
Store the API in a text file. Then, use jQuery $.get() to retrieve it. Make sure the configure your .htaccess file to disallow direct TXT file access.
To load Google Maps API dynamically, use $.getScript() in your code, right before you need the map.
Google also recommends restricting API usage by referral and/or IP address.

Using jquery need to display facebook user name in html page

Using jquery (javascript) need to display facebook user name in html page using facebook user id. I am not using facebook api. Any one have any idea.
Without using the facebook API (Graph API or older facebook REST API) either javascript or PHP there's no way you can achive this.
Facebook needs to authenticate your application before you ask for data.
You can use fb:name from FBML (Facebook Markup Language) to achive it easily. But as recoomended, you should not use FBML for new applications since the recommended way is the new Graph API. FBML is in the process of being deprecated.
Using the new Graph API you can get the JSON object for the user by fetching https://graph.facebook.com/{userID}
You need to use the Facebook API as Facebook needs to authenticate your application's requests for user data. (http://developers.facebook.com/docs/api#authorization)
I recommend using the Graph API (http://developers.facebook.com/docs/api), but it takes a bit of work getting started.
I use the PHP SDK (http://github.com/facebook/php-sdk/), though others also exist and you can find them in the Facebook Documentation.
However, to answer your question better, If you have your authorization token you can make calls for example:
https://graph.facebook.com/me?access_token=<Your Access token>
would return a JSON object containing your basic information, including your name, birthday, and basically everything that appears on your profile.
Before doing anything you MUST register your own application at www.facebook.com/developers/
(Edit: I was not aware FBML was being phased out)
I know it's an old thread, but so that people doesn't get mislead...
There is NO need for Facebook to authenticate your application before you ask for data like user name because user name is public info and does not require permission. Just call https://graph.facebook.com/{userID} and you will get a JSON object. You can achieve this easily using jquery:
$.get("https://graph.facebook.com/{userID}", function (data) {
alert(data.name);
}, 'jsonp');

Categories

Resources