Ember Google Analytics Embed is an addon for quickly building custom Google Analytics dashboards in your Ember.js app, using the Google Analytics Embed API.
The addon exposes the following components to use in your templates:
- Bar Chart (ga-embed-bar-chart)
- Column Chart (ga-embed-column-chart)
- Geo Chart (ga-embed-geo-chart)
- Line Chart (ga-embed-line-chart)
- Pie Chart (ga-embed-pie-chart)
- Table (ga-embed-table)
The addon also enables Analytics user authorization using the authorization (ga-embed-authorize) component and view selection via the view selector (ga-embed-view-selector) component.
Check out the Embed API demos page for examples.
ember install ember-google-analytics-embed
To authorize your application to access the Google Analytics API, you must create a Client ID from the Google API Console. When you've obtained your Client ID, add it to your environment file. If you are using Geo Charts, you must also include an API Key, for accessing the Google Maps Javascript API. Both these APIs must be enabled from your API console and have the right origins/referrers set.
// config/environment.js
ENV['google-analytics-embed'] = {
clientId: 'YOUR_CLIENT_ID',
apiKey: 'YOUR_API_KEY'
};
There are two ways to authorize users: through the authorization flow, requiring a user to click and authenticate, or via an access token sent from your server.
Adding the base ga-embed-authorize
component to your templates will create a 'Sign in to Google Analytics' button and handle the authorization flow automatically:
Inject the ga-embed
service into your templates to conditionally show/hide elements:
The ga-embed
service also exposes information about the current authorized user:
To enable the user to sign out, inject the ga-embed
service into the consuming object and create the following action.
gaEmbed: service(),
actions: {
signOut() {
get(this, 'gaEmbed').signOut().then(() => {
// Returns a promise when complete
});
}
}
To remove the user auth flow, you may return an access token from your server. Simply pass the access token to the ga-embed-authorize
component. You can find more information on setting up server side authorization here.
Each visualization accepts two main attributes, a query and an options hash.
To get data from our Google Analytics property, we must build a query using the Google Reporting API. The example below shows a pie chart of sessions, segmented by country. It limits the data to the last 30 days up until today and requests just the top ten results.
An options hash may be passed to each chart, allowing full configuration of the visualization.
Individual options properties may also be passed and can be dynamically updated.
Creates a bar chart visualization and accepts the following configuration options.
Creates a column chart visualization and accepts the following configuration options.
Creates a geo chart visualization and accepts the following configuration options. This component lazy-loads the Google Maps Geocoding API.
The region property can be dynamically updated and is validated before the chart is updated to ensure a valid region code is used.
Creates a line chart visualization and accepts the following configuration options.
Creates a pie chart visualization and accepts the following configuration options.
To transform a pie chart into a donut chart, simply add a value for the pie hole.
Creates a table visualization and accepts the following configuration options.
Each visualization has a loading state class of .ga-embed-visualization-loading
, which you can customize in your CSS. The classes set on visualizations allow for custom loading states per visualization.
<div id="ember123" class="ga-embed-visualization ga-embed-chart ga-embed-line-chart ga-embed-visualization-loading">...</div>
By default, visualizations auto resize on window resize. To disable auto resizing, set responsiveResize=false
on the visualization.
When dynamically updating many properties on a visualization, it may be beneficial to debounce executing a new render. To do so, the visualization accepts a debounce
value in milliseconds (ms).
The ga-embed-view-selector
component allows the user to select a view from any property they are authorized on. Add the view selector component to your template.
Use the mutable property in your queries:
The gaEmbed
service enables a quick method to query data from analytics without directly using a visualization. This can be useful for querying the Google Analytics Reporting API and using the data in your own custom components.
get(this, 'gaEmbed').getData({
'ids': 'YOUR_PROPERTY_ID',
'metrics': 'ga:sessions',
'dimensions': 'ga:date',
'start-date': '30daysAgo',
'end-date': 'yesterday'
}).then(data => {
console.log(data);
}).catch(err => {
console.log(err);
});
A crude dummy app demonstrates all the functionality of the addon. To view the dummy app, clone the repo and simply create a .env file with the following information:
# .env
GOOGLE_API_KEY=YOUR_API_KEY
GOOGLE_CLIENT_ID=YOUR_CLIENT_ID
Then start the server using:
ember serve