Skip to content

Commit

Permalink
Merge pull request #19 from pulse00/master
Browse files Browse the repository at this point in the history
Added google dashboard library
  • Loading branch information
matthewfitz committed May 15, 2012
2 parents de092c0 + eda53a7 commit 8cadb31
Show file tree
Hide file tree
Showing 7 changed files with 774 additions and 1 deletion.
35 changes: 34 additions & 1 deletion Analytics.php
Expand Up @@ -21,12 +21,19 @@ class Analytics
private $pageViewsWithBaseUrl = true;
private $trackers;
private $whitelist;
private $api_key;
private $client_id;
private $table_id;

public function __construct(ContainerInterface $container, array $trackers = array(), array $whitelist = array())
public function __construct(ContainerInterface $container,
array $trackers = array(), array $whitelist = array(), array $dashboard = array())
{
$this->container = $container;
$this->trackers = $trackers;
$this->whitelist = $whitelist;
$this->api_key = isset($dashboard['api_key']) ? $dashboard['api_key'] : '';
$this->client_id = isset($dashboard['client_id']) ? $dashboard['client_id'] : '';
$this->table_id = isset($dashboard['table_id']) ? $dashboard['table_id'] : '';
}

public function excludeBaseUrl()
Expand Down Expand Up @@ -427,4 +434,30 @@ private function getTransactionFromSession()
{
return $this->container->get('session')->get(self::TRANSACTION_KEY);
}

/**
*
* @return string
*/
public function getApiKey()
{
return $this->api_key;
}

/**
*
* @return string
*/
public function getClientId()
{
return $this->client_id;
}

/**
* @return string
*/
public function getTableId()
{
return $this->table_id;
}
}
3 changes: 3 additions & 0 deletions DependencyInjection/GoogleExtension.php
Expand Up @@ -65,6 +65,9 @@ private function analyticsLoad(array $configs, ContainerBuilder $container)
if (isset($config['trackers'])) {
$container->setParameter('google.analytics.trackers', $config['trackers']);
}
if (isset($config['dashboard'])) {
$container->setParameter('google.analytics.dashboard', $config['dashboard']);
}
if (isset($config['whitelist'])) {
$container->setParameter('google.analytics.whitelist', $config['whitelist']);
}
Expand Down
15 changes: 15 additions & 0 deletions Helper/AnalyticsHelper.php
Expand Up @@ -110,6 +110,21 @@ public function getTrackers(array $trackers = array())
{
return $this->analytics->getTrackers($trackers);
}

public function getApiKey()
{
return $this->analytics->getApiKey();
}

public function getClientId()
{
return $this->analytics->getClientId();
}

public function getTableId()
{
return $this->analytics->getTableId();
}

public function isTransactionValid()
{
Expand Down
1 change: 1 addition & 0 deletions Resources/config/analytics.xml
Expand Up @@ -15,6 +15,7 @@
<argument type="service" id="service_container" />
<argument>%google.analytics.trackers%</argument>
<argument>%google.analytics.whitelist%</argument>
<argument>%google.analytics.dashboard%</argument>
</service>

<service id="templating.helper.google_analytics" class="AntiMattr\GoogleBundle\Helper\AnalyticsHelper">
Expand Down
88 changes: 88 additions & 0 deletions Resources/docs/dashboard.md
@@ -0,0 +1,88 @@
Google Dashboard API
====================

The bundle adds support for the [google dashboard library](http://googledevelopers.blogspot.com/2012/05/new-google-analytics-easy-dashboard.html).

## Usage


### Configuration

Add your google api credentials in your configuration (see the above link how to set it up):

``` yml
google:
analytics:
# .... other settings
dashboard:
api_key: your-api-key
client_id: your-client-id
table_id: your-table-id
```

### Initializtation

In your dashboard, simply include the `dashboard.html.twig` template with an optional `initCallback` parameter:

``` jinja
{% include "GoogleBundle:Analytics:dashboard.html.twig" with { 'initCallback' : 'myDashCallback' } %}
```

The `myDashCallback` is a name of a javascript function which is being called after the google dashboard library has been initialized.


### Authorization

The dashboard library needs a button to initialize the google authorization, this defaults to the id `authorize-button`:

``` html
<button id="authorize-button" style="visibility:hidden">Authorize Google Analytics</button>
```

The id of the element can be configured by passing a `authorizeButton` parameter to the dashboard twig template.

### Drawing charts

Once the user is authorized at google, you can start drawing your analytics charts in your templates:

``` html

<div class="gadash-container">
<h3>Visits</h3>
<div id='dataOverTimeConfig'></div>
<div>

<script type="text/javascript">
function adminInit() {
// Create new Chart.
var dataOverTime = new gadash.Chart({
'last-n-days': 30,
'chartOptions': {
width: 700
},
'divContainer': 'dataOverTimeConfig',
'type': 'LineChart',
'query': {
'dimensions': 'ga:date',
'sort': 'ga:date',
'metrics': 'ga:visitors, ga:visits, ga:pageviews',
'ids' : gadash.tableId
},
'chartOptions': {
height: 300,
legend: {position: 'bottom'},
hAxis: {title:'Date'},
curveType: 'function'
}
}).render();
}
</script>
```

Note that you need to pass the `gadash.tableId` in each chart, otherwise you'll get an api error.



0 comments on commit 8cadb31

Please sign in to comment.