Skip to content

Commit d5f5da6

Browse files
author
Ace Nassri
authored
feat(functions): add firebase analytics sample (GoogleCloudPlatform#1301)
1 parent 27698a1 commit d5f5da6

File tree

5 files changed

+199
-0
lines changed

5 files changed

+199
-0
lines changed

CODEOWNERS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,6 @@
1515
/firestore/ @GoogleCloudPlatform/firestore-dpe @GoogleCloudPlatform/php-admins
1616
/iot/ @gcseh @GoogleCloudPlatform/api-iot @GoogleCloudPlatform/php-admins
1717
/storage/ @GoogleCloudPlatform/storage-dpe @GoogleCloudPlatform/php-admins
18+
19+
# Functions samples owned by the Firebase team
20+
/functions/firebase_analytics @schandel @samtstern
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"require": {
3+
"google/cloud-functions-framework": "^0.7.2"
4+
},
5+
"scripts": {
6+
"start": [
7+
"Composer\\Config::disableProcessTimeout",
8+
"FUNCTION_SIGNATURE_TYPE=cloudevent FUNCTION_TARGET=firebaseAnalytics php -S localhost:${PORT:-8080} vendor/bin/router.php"
9+
]
10+
}
11+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
2+
/**
3+
* Copyright 2021 Google LLC.
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+
// [START functions_firebase_analytics]
19+
20+
use Google\CloudFunctions\CloudEvent;
21+
22+
function firebaseAnalytics(CloudEvent $cloudevent): void
23+
{
24+
$log = fopen(getenv('LOGGER_OUTPUT') ?: 'php://stderr', 'wb');
25+
26+
$data = $cloudevent->getData();
27+
28+
fwrite($log, 'Function triggered by the following event:' . $data['resource'] . PHP_EOL);
29+
30+
$analyticsEvent = $data['eventDim'][0];
31+
$unixTime = $analyticsEvent['timestampMicros'] / 1000;
32+
33+
fwrite($log, 'Name: ' . $analyticsEvent['name'] . PHP_EOL);
34+
fwrite($log, 'Timestamp: ' . gmdate("Y-m-d\TH:i:s\Z", $unixTime) . PHP_EOL);
35+
36+
$userObj = $data['userDim'];
37+
fwrite($log, sprintf(
38+
'Location: %s, %s' . PHP_EOL,
39+
$userObj['geoInfo']['city'],
40+
$userObj['geoInfo']['country']
41+
));
42+
43+
fwrite($log, 'Device Model: %s' . $userObj['deviceInfo']['deviceModel'] . PHP_EOL);
44+
}
45+
// [END functions_firebase_analytics]
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
xml version="1.0" encoding="UTF-8"?>
2+
17+
<phpunit bootstrap="../../testing/bootstrap.php" convertWarningsToExceptions="false">
18+
<testsuites>
19+
<testsuite name="Cloud Functions Firebase Remote Config Test Suite">
20+
<directory>testdirectory>
21+
testsuite>
22+
testsuites>
23+
<logging>
24+
<log type="coverage-clover" target="build/logs/clover.xml"/>
25+
logging>
26+
<filter>
27+
<whitelist>
28+
<directory suffix=".php">.directory>
29+
<exclude>
30+
<directory>./vendordirectory>
31+
exclude>
32+
whitelist>
33+
filter>
34+
phpunit>
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
2+
/**
3+
* Copyright 2021 Google LLC.
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+
declare(strict_types=1);
19+
20+
namespace Google\Cloud\Samples\Functions\FirebaseAnalytics\Test;
21+
22+
use PHPUnit\Framework\TestCase;
23+
use Google\CloudFunctions\CloudEvent;
24+
use Google\Cloud\TestUtils\CloudFunctionLocalTestTrait;
25+
26+
/**
27+
* Class IntegrationTest.
28+
*
29+
* Integration Test for firebaseAnalytics
30+
*/
31+
class IntegrationTest extends TestCase
32+
{
33+
use CloudFunctionLocalTestTrait;
34+
35+
/** @var string */
36+
private static $entryPoint = 'firebaseAnalytics';
37+
38+
/** @var string */
39+
private static $functionSignatureType = 'cloudevent';
40+
41+
public function dataProvider()
42+
{
43+
return [
44+
[
45+
'cloudevent' => CloudEvent::fromArray([
46+
'id' => uniqid(),
47+
'source' => 'firebase.googleapis.com',
48+
'specversion' => '1.0',
49+
'type' => 'google.firebase.remoteconfig.v1.updated',
50+
'data' => [
51+
// eventDim is a list of dictionaries
52+
'eventDim' => array([
53+
'name' => 'test_event',
54+
'timestampMicros' => time() * 1000,
55+
]),
56+
'userDim' => [
57+
'geoInfo' => [
58+
'city' => 'San Francisco',
59+
'country' => 'US'
60+
],
61+
'deviceInfo' => [
62+
'deviceModel' => 'Google Pixel XL'
63+
]
64+
]
65+
],
66+
]),
67+
'statusCode' => '200',
68+
],
69+
];
70+
}
71+
72+
/**
73+
* @dataProvider dataProvider
74+
*/
75+
public function testFirebaseAnalytics(
76+
CloudEvent $cloudevent,
77+
string $statusCode
78+
): void {
79+
// Send an HTTP request using CloudEvent.
80+
$resp = $this->request($cloudevent);
81+
82+
// The Cloud Function logs all data to stderr.
83+
$actual = self::$localhost->getIncrementalErrorOutput();
84+
85+
// Confirm the status code.
86+
$this->assertEquals($statusCode, $resp->getStatusCode());
87+
88+
// Verify the data properties are logged by the function.
89+
$data = $cloudevent->getData();
90+
foreach ($data as $property => $value) {
91+
if (is_string($value)) {
92+
$this->assertStringContainsString($value, $actual);
93+
}
94+
}
95+
foreach ($data['eventDim'] as $property => $value) {
96+
if (is_string($value)) {
97+
$this->assertStringContainsString($value, $actual);
98+
}
99+
}
100+
foreach ($data['userDim'] as $property => $value) {
101+
if (is_string($value)) {
102+
$this->assertStringContainsString($value, $actual);
103+
}
104+
}
105+
}
106+
}

0 commit comments

Comments
 (0)