Skip to content

Commit 64cc018

Browse files
authored
Add vNext snippets for Database, Storage, Messaging, and Analytics (#92)
2 parents b27016f + dbb0293 commit 64cc018

File tree

92 files changed

+3046
-2
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

92 files changed

+3046
-2
lines changed

analytics-next/ecommerce.js

Lines changed: 293 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,293 @@
1+
// [SNIPPET_REGISTRY disabled]
2+
// [SNIPPETS_SEPARATION enabled]
3+
4+
import { initializeApp } from "firebase/app";
5+
6+
const firebaseApp = initializeApp({
7+
apiKey: '### FIREBASE API KEY ###',
8+
appId: '### FIREBASE APP ID ###',
9+
projectId: '### FIREBASE PROJECT ID ###'
10+
});
11+
12+
13+
// [START analytics_ecommerce_items]
14+
// A pair of jeggings
15+
const item_jeggings = {
16+
item_id: 'SKU_123',
17+
item_name: 'jeggings',
18+
item_category: 'pants',
19+
item_variant: 'black',
20+
item_brand: 'Google',
21+
price: 9.99
22+
};
23+
24+
// A pair of boots
25+
const item_boots = {
26+
item_id: 'SKU_456',
27+
item_name: 'boots',
28+
item_category: 'shoes',
29+
item_variant: 'brown',
30+
item_brand: 'Google',
31+
price: 24.99
32+
};
33+
34+
// A pair of socks
35+
const item_socks = {
36+
item_id: 'SKU_789',
37+
item_name: 'ankle_socks',
38+
item_category: 'socks',
39+
item_variant: 'red',
40+
item_brand: 'Google',
41+
price: 5.99
42+
};
43+
// [END analytics_ecommerce_items]
44+
45+
function ecommerceViewItemList() {
46+
// [START analytics_ecommerce_view_item_list]
47+
const { getAnalytics, logEvent } = require("@firebase/analytics");
48+
49+
// Prepare ecommerce params
50+
const params1 = {
51+
item_list_id: 'L001',
52+
item_list_name: 'Related products',
53+
items: [item_jeggings, item_boots, item_socks]
54+
};
55+
56+
// Log event
57+
const analytics = getAnalytics(firebaseApp);
58+
logEvent(analytics, 'view_item_list', params1);
59+
// [END analytics_ecommerce_view_item_list]
60+
}
61+
62+
function ecommerceSelectItem() {
63+
// [START analytics_ecommerce_select_item]
64+
const { getAnalytics, logEvent } = require("@firebase/analytics");
65+
66+
// Prepare ecommerce event params
67+
const params2 = {
68+
item_list_id: 'L001',
69+
item_list_name: 'Related products',
70+
items: [item_jeggings]
71+
};
72+
73+
// Log event
74+
const analytics = getAnalytics(firebaseApp);
75+
logEvent(analytics, 'select_item', params2);
76+
// [END analytics_ecommerce_select_item]
77+
}
78+
79+
function ecommerceViewItemDetails() {
80+
// [START analytics_ecommerce_view_item_details]
81+
const { getAnalytics, logEvent } = require("@firebase/analytics");
82+
83+
// Prepare ecommerce event params
84+
const params3 = {
85+
currency: 'USD',
86+
value: 9.99,
87+
items: [item_jeggings]
88+
};
89+
90+
// Log event
91+
const analytics = getAnalytics(firebaseApp);
92+
logEvent(analytics, 'view_item', params3);
93+
// [END analytics_ecommerce_view_item_details]
94+
}
95+
96+
function ecommerceAddCart() {
97+
// [START analytics_ecommerce_add_cart]
98+
const { getAnalytics, logEvent } = require("@firebase/analytics");
99+
100+
// Specify order quantity
101+
const item_jeggings_quantity = {
102+
...item_jeggings,
103+
quantity: 2
104+
};
105+
106+
// Prepare ecommerce bundle
107+
const params4 = {
108+
currency: 'USD',
109+
value: 19.98,
110+
items: [item_jeggings_quantity]
111+
};
112+
113+
// Log event when a product is added to a wishlist
114+
const analytics = getAnalytics(firebaseApp);
115+
logEvent(analytics, 'add_to_wishlist', params4);
116+
117+
// Log event when a product is added to the cart
118+
logEvent(analytics, 'add_to_cart', params4);
119+
// [END analytics_ecommerce_add_cart]
120+
}
121+
122+
function ecommerceViewCart() {
123+
// [START analytics_ecommerce_view_cart]
124+
const { getAnalytics, logEvent } = require("@firebase/analytics");
125+
126+
// Specify order quantity
127+
const item_jeggings_quantity = {
128+
...item_jeggings,
129+
quantity: 2
130+
};
131+
132+
const item_boots_quantity = {
133+
...item_boots,
134+
quantity: 1
135+
};
136+
137+
// Prepare ecommerce params
138+
const params5 = {
139+
currency: 'USD',
140+
value: 44.97,
141+
items: [item_jeggings_quantity, item_boots_quantity]
142+
};
143+
144+
// Log event when the cart is viewed
145+
const analytics = getAnalytics(firebaseApp);
146+
logEvent(analytics, 'view_cart', params5);
147+
// [END analytics_ecommerce_view_cart]
148+
}
149+
150+
function ecommerceRemoveCart() {
151+
// [START analytics_ecommerce_remove_cart]
152+
const { getAnalytics, logEvent } = require("@firebase/analytics");
153+
154+
// Prepare ecommerce params
155+
const params6 = {
156+
currency: 'USD',
157+
value: 24.99,
158+
items: [item_jeggings]
159+
};
160+
161+
// Log event
162+
const analytics = getAnalytics(firebaseApp);
163+
logEvent(analytics, 'remove_from_cart', params6);
164+
// [END analytics_ecommerce_remove_cart]
165+
}
166+
167+
function ecommerceCheckout() {
168+
// [START analytics_ecommerce_checkout]
169+
const { getAnalytics, logEvent } = require("@firebase/analytics");
170+
171+
// Prepare ecommerce params
172+
const params7 = {
173+
currency: 'USD',
174+
value: 14.98, // Total Revenue
175+
coupon: 'SUMMER_FUN',
176+
items: [item_jeggings]
177+
};
178+
179+
// Log event
180+
const analytics = getAnalytics(firebaseApp);
181+
logEvent(analytics, 'begin_checkout', params7);
182+
// [END analytics_ecommerce_checkout]
183+
}
184+
185+
function ecommerceShippingInfo() {
186+
// [START analytics_ecommerce_shipping_info]
187+
const { getAnalytics, logEvent } = require("@firebase/analytics");
188+
189+
// Prepare ecommerce params
190+
const params8 = {
191+
currency: 'USD',
192+
value: 14.98, // Total Revenue
193+
coupon: 'SUMMER_FUN',
194+
shipping_tier: 'Ground',
195+
items: [item_jeggings]
196+
};
197+
198+
// Log event
199+
const analytics = getAnalytics(firebaseApp);
200+
logEvent(analytics, 'add_shipping_info', params8);
201+
// [END analytics_ecommerce_shipping_info]
202+
}
203+
204+
function ecommercePaymentInfo() {
205+
// [START analytics_ecommerce_payment_info]
206+
const { getAnalytics, logEvent } = require("@firebase/analytics");
207+
208+
// Prepare ecommerce params
209+
const params9 = {
210+
currency: 'USD',
211+
value: 14.98, // Total Revenue
212+
coupon: 'SUMMER_FUN',
213+
payment_type: 'Visa',
214+
items: [item_jeggings]
215+
};
216+
217+
// Log event
218+
const analytics = getAnalytics(firebaseApp);
219+
logEvent(analytics, 'add_payment_info', params9);
220+
// [END analytics_ecommerce_payment_info]
221+
}
222+
223+
function ecommercePurchase() {
224+
// [START analytics_ecommerce_purchase]
225+
const { getAnalytics, logEvent } = require("@firebase/analytics");
226+
227+
// Prepare ecommerce bundle
228+
const params10 = {
229+
transaction_id: 'T12345',
230+
affiliation: 'Google Store',
231+
currency: 'USD',
232+
value: 14.98, // Total Revenue
233+
tax: 2.85,
234+
shipping: 5.34,
235+
coupon: 'SUMMER_FUN',
236+
items: [item_jeggings]
237+
};
238+
239+
// Log event
240+
const analytics = getAnalytics(firebaseApp);
241+
logEvent(analytics, 'purchase', params10);
242+
// [END analytics_ecommerce_purchase]
243+
}
244+
245+
function ecommerceRefund() {
246+
// [START analytics_ecommerce_refund]
247+
const { getAnalytics, logEvent } = require("@firebase/analytics");
248+
249+
// Prepare ecommerce params
250+
const params11 = {
251+
transaction_id: 'T12345', // Required
252+
affiliation: 'Google Store',
253+
currency: 'USD',
254+
value: 9.99,
255+
items: []
256+
};
257+
258+
// (Optional) For partial refunds, define the item_id and quantity of refunded items
259+
const refundedProduct = {
260+
item_id: 'SKU_123', // Required
261+
quantity: 1 // Required
262+
};
263+
264+
params11.items.push(refundedProduct);
265+
266+
// Log event
267+
const analytics = getAnalytics(firebaseApp);
268+
logEvent(analytics, 'refund', params11);
269+
// [END analytics_ecommerce_refund]
270+
}
271+
272+
function ecommercePromotions() {
273+
// [START analytics_ecommerce_promotions]
274+
const { getAnalytics, logEvent } = require("@firebase/analytics");
275+
276+
// Prepare ecommerce params
277+
const params12 = {
278+
promotion_id: 'ABC123',
279+
promotion_name: 'Summer Sale',
280+
creative_name: 'summer2020_promo.jpg',
281+
creative_slot: 'featured_app_1',
282+
location_id: 'HERO_BANNER',
283+
items: [item_jeggings]
284+
};
285+
286+
// Log event when a promotion is displayed
287+
const analytics = getAnalytics(firebaseApp);
288+
logEvent(analytics, 'view_promotion', params12);
289+
290+
// Log event when a promotion is selected
291+
logEvent(analytics, 'select_promotion', params12);
292+
// [END analytics_ecommerce_promotions]
293+
}

analytics-next/index.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// [SNIPPET_REGISTRY disabled]
2+
// [SNIPPETS_SEPARATION enabled]
3+
4+
import { initializeApp } from "firebase/app";
5+
6+
const firebaseApp = initializeApp({
7+
apiKey: '### FIREBASE API KEY ###',
8+
appId: '### FIREBASE APP ID ###',
9+
projectId: '### FIREBASE PROJECT ID ###'
10+
});
11+
12+
function initialize() {
13+
// [START analytics_initialize]
14+
const { getAnalytics } = require("firebase/analytics");
15+
16+
const analytics = getAnalytics(firebaseApp);
17+
// [END analytics_initialize]
18+
}
19+
20+
function logEvent() {
21+
// [START analytics_log_event]
22+
const { getAnalytics, logEvent } = require("firebase/analytics");
23+
24+
const analytics = getAnalytics(firebaseApp);
25+
logEvent(analytics, 'notification_received');
26+
// [END analytics_log_event]
27+
}
28+
29+
function logEventParams() {
30+
// [START analytics_log_event_params]
31+
const { getAnalytics, logEvent } = require("firebase/analytics");
32+
33+
const analytics = getAnalytics(firebaseApp);
34+
logEvent(analytics, 'select_content', {
35+
content_type: 'image',
36+
content_id: 'P12453',
37+
items: [{ name: 'Kittens' }]
38+
});
39+
// [END analytics_log_event_params]
40+
}
41+
42+
function logEventCustomParams() {
43+
// [START analytics_log_event_custom_params]
44+
const { getAnalytics, logEvent } = require("firebase/analytics");
45+
46+
const analytics = getAnalytics(firebaseApp);
47+
logEvent(analytics, 'goal_completion', { name: 'lever_puzzle'});
48+
// [END analytics_log_event_custom_params]
49+
}
50+
51+
function setUserProperties() {
52+
// [START analytics_set_user_properties]
53+
const { getAnalytics, setUserProperties } = require("firebase/analytics");
54+
55+
const analytics = getAnalytics(firebaseApp);
56+
setUserProperties(analytics, { favorite_food: 'apples' });
57+
// [END analytics_set_user_properties]
58+
}

analytics-next/package.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "analytics-next",
3+
"version": "1.0.0",
4+
"scripts": {
5+
"compile": "cp ../tsconfig.json.template ./tsconfig.json && tsc"
6+
},
7+
"license": "Apache-2.0",
8+
"dependencies": {
9+
"firebase": "exp"
10+
}
11+
}

database-next/emulator-suite.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// [SNIPPET_REGISTRY disabled]
2+
// [SNIPPETS_SEPARATION enabled]
3+
4+
import { initializeApp } from "firebase/app";
5+
6+
const firebaseApp = initializeApp({
7+
apiKey: '### FIREBASE API KEY ###',
8+
appId: '### FIREBASE APP ID ###',
9+
projectId: '### FIREBASE PROJECT ID ###'
10+
});
11+
12+
function onDocumentReady() {
13+
// [START rtdb_emulator_connect]
14+
const { getDatabase } = require("firebase/database");
15+
16+
const db = getDatabase(firebaseApp);
17+
if (location.hostname === "localhost") {
18+
// Point to the RTDB emulator running on localhost.
19+
db.useEmulator("localhost", 9000);
20+
}
21+
// [END rtdb_emulator_connect]
22+
}
23+
24+
function flushRealtimeDatabase() {
25+
// [START rtdb_emulator_flush]
26+
const { getDatabase } = require("firebase/database");
27+
28+
// With a database Reference, write null to clear the database.
29+
const db = getDatabase(firebaseApp);
30+
db.ref().set(null);
31+
// [END rtdb_emulator_flush]
32+
}

0 commit comments

Comments
 (0)