-
Notifications
You must be signed in to change notification settings - Fork 275
feat: google analytics (APP-105) #41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6f279d1
feat: google analytics
astansler d9c48ef
wip: pr
astansler 6342e74
wip: error codes by virtual urls
astansler ba019bd
feat: uncaught excetion handle, logs for analytics errors
astansler 2e8c0b2
Merge branch 'master' into dev-ga
astansler File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| // Copyright 2017 Sourcerer Inc. All Rights Reserved. | ||
| // Author: Anatoly Kislov (anatoly@sourcerer.io) | ||
|
|
||
| package app | ||
|
|
||
| import com.github.kittinunf.fuel.core.FuelError | ||
| import com.github.kittinunf.fuel.core.FuelManager | ||
| import com.github.kittinunf.fuel.core.Method | ||
| import com.github.kittinunf.fuel.core.Request | ||
| import com.google.protobuf.InvalidProtocolBufferException | ||
| import java.security.InvalidParameterException | ||
|
|
||
| typealias Param = Pair<String, String> | ||
|
|
||
| /** | ||
| * Google Analytics events tracking. | ||
| */ | ||
| object Analytics { | ||
| private val IS_ENABLED = BuildConfig.IS_GA_ENABLED | ||
| private val BASE_PATH = BuildConfig.GA_BASE_PATH | ||
| private val BASE_URL = "/virtual/app/" | ||
| private val PROTOCOL_VERSION = "1" | ||
| private val TRACKING_ID = BuildConfig.GA_TRACKING_ID | ||
| private val DATA_SOURCE = "app" | ||
|
|
||
| private val HIT_PAGEVIEW = "pageview" | ||
| private val HIT_EXCEPTION = "exception" | ||
|
|
||
| private val fuelManager = FuelManager() | ||
|
|
||
| var uuid: String = "" // Should be set on start of the app. | ||
| var username: String = "" // Should be set on successful authorization. | ||
|
|
||
| init { | ||
| fuelManager.basePath = BASE_PATH | ||
| } | ||
|
|
||
| private fun post(params: List<Param>): Request { | ||
| return fuelManager.request(Method.POST, "/collect", params) | ||
| } | ||
|
|
||
| /** | ||
| * Google Analytics Measurement Protocol is used to track events. | ||
| * User iteration data is sent to GA endpoint via POST request. | ||
| * Events (or hits) mapped to virtual urls with "Data Source" parameter. | ||
| * Used parameters: | ||
| * - v: Protocol Version (Required) | ||
| * - tid: Tracking ID - used to specify GA account (Required) | ||
| * - cid: Client ID - anonymous client id (UUID type 4) | ||
| * - uid: User ID - username | ||
| * - t: Hit Type - type of event | ||
| * - dp: Document Path - virtual url | ||
| */ | ||
| private fun trackEvent(event: String, params: List<Param> = listOf()) { | ||
| if (!IS_ENABLED || (username.isEmpty() && uuid.isEmpty())) { | ||
| return | ||
| } | ||
|
|
||
| val idParams = mutableListOf<Param>() | ||
| if (uuid.isNotEmpty()) { | ||
| idParams.add("cid" to uuid) | ||
| } | ||
| if (username.isNotEmpty()) { | ||
| idParams.add("uid" to username) | ||
| } | ||
|
|
||
| val defaultParams = listOf("v" to PROTOCOL_VERSION, | ||
| "tid" to TRACKING_ID, | ||
| "ds" to DATA_SOURCE, | ||
| "t" to HIT_PAGEVIEW, | ||
| "dp" to BASE_URL + event) | ||
|
|
||
| try { | ||
| // Send event to GA with united params. | ||
| post(params + defaultParams.filter { !params.contains(it) } | ||
| + idParams).responseString() | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. weird line breakdown
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why? |
||
| } catch (e: Throwable) { | ||
| Logger.error("Error while sending error report", e, logOnly = true) | ||
| } | ||
| } | ||
|
|
||
| fun trackStart() { | ||
| trackEvent("start") | ||
| } | ||
|
|
||
| fun trackAuth() { | ||
| trackEvent("auth") | ||
| } | ||
|
|
||
| fun trackConfigSetup() { | ||
| trackEvent("config/setup") | ||
| } | ||
|
|
||
| fun trackConfigChanged() { | ||
| trackEvent("config/changed") | ||
| } | ||
|
|
||
| fun trackHashingRepoSuccess() { | ||
| trackEvent("hashing/repo/success") | ||
| } | ||
|
|
||
| fun trackHashingSuccess() { | ||
| trackEvent("hashing/success") | ||
| } | ||
|
|
||
| fun trackError(e: Throwable? = null, code: String = "") { | ||
| val url = if (e != null) getErrorUrl(e) else code | ||
| val separator = if (url.isNotEmpty()) "/" else "" | ||
| trackEvent("error" + separator + url, listOf("t" to HIT_EXCEPTION)) | ||
| } | ||
|
|
||
| fun trackExit() { | ||
| trackEvent("exit") | ||
| } | ||
|
|
||
| private fun getErrorUrl(e: Throwable): String { | ||
| // Mapping for request exceptions. | ||
| when (e) { | ||
| is FuelError -> return "request" | ||
| is InvalidParameterException -> return "request/parsing" | ||
| is InvalidProtocolBufferException -> return "request/parsing" | ||
| } | ||
|
|
||
| // Get concrete class of exception name removing all common parts. | ||
| val name = e.javaClass.simpleName.replace("Exception", "") | ||
| .replace("Error", "") | ||
| .replace("Throwable", "") | ||
|
|
||
| if (name.length == 0 || name.length == 1) { | ||
| return name | ||
| } | ||
|
|
||
| // Divide CamelCased words in class name by dashes. | ||
| val nameCapitalized = name.toUpperCase() | ||
| var url = name[0].toString() | ||
| for (i in 1..name.length - 1) { | ||
| if (name[i] == nameCapitalized[i]) { | ||
| url += "-" | ||
| } | ||
| url += name[i] | ||
| } | ||
|
|
||
| return url.toLowerCase() | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
header
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok.