|
| 1 | +# Examples |
| 2 | +The following document assumes that you have a working [QtFirebase setup](https://github.com/Larpon/QtFirebase/SETUP.md) |
| 3 | + |
| 4 | +## Analytics |
| 5 | + |
| 6 | +### QML |
| 7 | +``` |
| 8 | +import QtFirebase 1.0 |
| 9 | +
|
| 10 | +Analytics { |
| 11 | + id: analytics |
| 12 | +
|
| 13 | + // Analytics collection enabled |
| 14 | + enabled: true |
| 15 | +
|
| 16 | + // App needs to be open at least 1s before logging a valid session |
| 17 | + minimumSessionDuration: 1000 |
| 18 | + // App session times out after 5s (5 seconds = 5000 milliseconds) |
| 19 | + sessionTimeout: 5000 |
| 20 | +
|
| 21 | + // Set the user ID: |
| 22 | + // NOTE the user id can't be more than 36 chars long |
| 23 | + //userId: "A_VERY_VERY_VERY_VERY_VERY_VERY_LONG_USER_ID_WILL_BE_TRUNCATED" |
| 24 | + userId: "qtfirebase_test_user" |
| 25 | + // or call setUserId() |
| 26 | +
|
| 27 | + // Unset the user ID: |
| 28 | + // userId: "" or call "unsetUserId()" |
| 29 | +
|
| 30 | + // Set user properties: |
| 31 | + // Max 25 properties allowed by Google |
| 32 | + // See https://firebase.google.com/docs/analytics/cpp/properties |
| 33 | + userProperties: [ |
| 34 | + { "sign_up_method" : "Google" }, |
| 35 | + { "qtfirebase_power_user" : "yes" }, |
| 36 | + { "qtfirebase_custom_property" : "test_value" } |
| 37 | + ] |
| 38 | + // or call setUserProperty() |
| 39 | +
|
| 40 | + onReadyChanged: { |
| 41 | + // See: https://firebase.google.com/docs/analytics/cpp/events |
| 42 | + analytics.logEvent("qtfb_ready_event") |
| 43 | + analytics.logEvent("qtfb_ready_event","string_test","string") |
| 44 | + analytics.logEvent("qtfb_ready_event","int_test",getRandomInt(-100, 100)) |
| 45 | + analytics.logEvent("qtfb_ready_event","double_test",getRandomArbitrary(-2.1, 2.7)) |
| 46 | +
|
| 47 | + analytics.logEvent("qtfb_ready_event_bundle",{ |
| 48 | + 'key_one': 'value', |
| 49 | + 'key_two': 14, |
| 50 | + 'key_three': 2.3 |
| 51 | + }) |
| 52 | + } |
| 53 | +} |
| 54 | +``` |
| 55 | + |
| 56 | +## Remote Config |
| 57 | +Feature information available at https://firebase.google.com/docs/remote-config/ |
| 58 | + |
| 59 | +### QML |
| 60 | +Remember to |
| 61 | +* Add parameters in remote config section at google [firebase console](https://console.firebase.google.com) |
| 62 | + |
| 63 | +``` |
| 64 | +import QtFirebase 1.0 |
| 65 | +
|
| 66 | +RemoteConfig{ |
| 67 | + id: remoteConfig |
| 68 | +
|
| 69 | + //1. Initialize parameters you would like to fetch from server and their default values |
| 70 | + parameters: { |
| 71 | + "TestString" : "test", |
| 72 | + "TestDouble" : 2.56, |
| 73 | + "TestLong" : 1100, |
| 74 | + "TestBool" : true |
| 75 | + } |
| 76 | +
|
| 77 | + //2. Set cache expiration time in milliseconds, see step 3 for details about cache |
| 78 | + cacheExpirationTime: 12*3600*1000 //12 hours in milliseconds (suggested as default in firebase) |
| 79 | +
|
| 80 | + //3. When remote config properly initialized request data from server |
| 81 | + onReadyChanged: { |
| 82 | + console.log("RemoteConfig ready changed:"+ready); |
| 83 | + if(ready) |
| 84 | + { |
| 85 | + remoteConfig.fetch(); |
| 86 | + //If the data in the cache was fetched no longer than cacheExpirationTime ago, |
| 87 | + //this method will return the cached data. If not, a fetch from the |
| 88 | + //Remote Config Server will be attempted. |
| 89 | + //If you need to get data urgent use fetchNow(), it is equal to fetch() call with cacheExpirationTime=0 |
| 90 | + //Be careful with urgent requests, too often requests will result to server throthling |
| 91 | + //which means it will refuse connections for some time |
| 92 | + } |
| 93 | + } |
| 94 | +
|
| 95 | + //4. If data was retrieved (both from server or cache) the handler will be called |
| 96 | + //you can access data by accessing the "parameters" member variable |
| 97 | +
|
| 98 | + onParametersChanged:{ |
| 99 | + console.log("RemoteConfig TestString:" + parameters['TestString']); |
| 100 | + console.log("RemoteConfig TestDouble:" + parameters['TestDouble']); |
| 101 | + console.log("RemoteConfig TestLong:" + parameters['TestLong']); |
| 102 | + console.log("RemoteConfig TestBool:" + parameters['TestBool']); |
| 103 | + } |
| 104 | +
|
| 105 | + //5. Handle errors |
| 106 | + onError:{ |
| 107 | + console.log("RemoteConfig error code:" + code + " message:" + message); |
| 108 | + } |
| 109 | +} |
| 110 | +``` |
0 commit comments