Skip to content

Commit dee2083

Browse files
1 parent 9b611fb commit dee2083

File tree

11 files changed

+2305
-0
lines changed

11 files changed

+2305
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Google Analytics and Google App Engine Flexible Environment
2+
3+
This sample application demonstrates how track events with Google Analytics
4+
when running in Google App Engine Flexible Environment.
5+
6+
## Prerequisites
7+
8+
- Install [`composer`](https://getcomposer.org)
9+
- Install dependencies by running:
10+
11+
```sh
12+
$ composer install
13+
```
14+
15+
## Deploy to App Engine
16+
17+
**Prerequisites**
18+
19+
- Install the [Google Cloud SDK](https://developers.google.com/cloud/sdk/).
20+
21+
- [Create a Google Analytics Property and obtain the Tracking ID](
22+
https://support.google.com/analytics/answer/1042508?ref_topic=1009620).
23+
Include the environment variables in app.yaml with your Tracking ID.
24+
For example:
25+
26+
```
27+
env_variables:
28+
GA_TRACKING_ID: your-tracking-id
29+
```
30+
31+
Before running the sample app locally, set the environment variables required by the app:
32+
33+
```
34+
export GA_TRACKING_ID=your-tracking-id
35+
```
36+
37+
**Deploy with gcloud**
38+
39+
```
40+
$ gcloud config set project YOUR_PROJECT_ID
41+
$ gcloud preview app deploy
42+
```

appengine/flexible/analytics/app.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
2+
/**
3+
* Copyright 2015 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
use GuzzleHttp\Client;
19+
use Silex\Application;
20+
use Silex\Provider\TwigServiceProvider;
21+
use Symfony\Component\HttpFoundation\Request;
22+
23+
// create the Silex application
24+
$app = new Application();
25+
$app->register(new TwigServiceProvider());
26+
$app['twig.path'] = [ __DIR__ ];
27+
28+
$app->get('/', function (Application $app, Request $request) {
29+
/** @var Twig_Environment $twig */
30+
$twig = $app['twig'];
31+
$baseUri = 'http://www.google-analytics.com/';
32+
$client = new GuzzleHttp\Client(['base_uri' => $baseUri]);
33+
$form = [
34+
'v' => '1', # API Version.
35+
'tid' => $app['GA_TRACKING_ID'], # Tracking ID / Property ID.
36+
# Anonymous Client Identifier. Ideally, this should be a UUID that
37+
# is associated with particular user, device, or browser instance.
38+
'cid' => '555',
39+
't' => 'event', # Event hit type.
40+
'ec' => 'Poker', # Event category.
41+
'ea' => 'Royal Flush', # Event action.
42+
'el' => 'Hearts', # Event label.
43+
'ev' => 0, # Event value, must be an integer
44+
];
45+
$response = $client->request('POST', 'collect', ['form_params' => $form]);
46+
return $twig->render('index.html.twig', [
47+
'base_uri' => $baseUri,
48+
'response_code' => $response->getStatusCode(),
49+
'response_reason' => $response->getReasonPhrase()]);
50+
});
51+
52+
return $app;

appengine/flexible/analytics/app.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
runtime: php
2+
vm: true
3+
4+
runtime_config:
5+
document_root: web
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"require": {
3+
"silex/silex": "^1.3",
4+
"twig/twig": "^1.24",
5+
"guzzlehttp/guzzle": "^6.2",
6+
"symfony/css-selector": "^3.1"
7+
},
8+
"require-dev": {
9+
"phpunit/phpunit": "~4",
10+
"google/cloud-tools": "^0.1.0"
11+
}
12+
}

0 commit comments

Comments
 (0)