Skip to content

Commit

Permalink
Merge pull request #9 from shortcut/master
Browse files Browse the repository at this point in the history
Version 4.1.1
  • Loading branch information
Morteza-Rastgoo committed Sep 10, 2021
2 parents 7f115fe + ec6a640 commit e1a2af0
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 17 deletions.
2 changes: 1 addition & 1 deletion libmobiletagging/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ apply plugin: 'com.github.dcendents.android-maven'

group='myGroups'

version = "4.1.0"
version = "4.1.1"


android {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
package se.kantarsifo.mobileanalytics.framework

import android.content.Context
import android.util.Log
import androidx.activity.ComponentActivity
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import se.kantarsifo.mobileanalytics.framework.Logger.fatalError
import se.kantarsifo.mobileanalytics.framework.Logger.log
import se.kantarsifo.mobileanalytics.framework.TagStringsAndValues.SIFO_APP_START_EVENT_CATEGORY
import se.kantarsifo.mobileanalytics.framework.Utils.getApplicationVersion
import se.kantarsifo.mobileanalytics.framework.Utils.isPackageInstalled
import java.net.HttpCookie

Expand Down Expand Up @@ -45,8 +46,9 @@ internal class TSMobileAnalyticsBackend : TSMobileAnalytics {
fatalError("Mobile Application Tagging Framework Failed to initiate - context must not be null")
return frameworkInstance
}
val version = BuildConfig.VERSION_NAME
val cookieValue = "trackPanelistOnly=$onlyPanelist&isWebViewBased=$isWebBased&sdkVersion=$version"
val sdkVersion = BuildConfig.VERSION_NAME
val appVersion = activity.getApplicationVersion()
val cookieValue = "trackPanelistOnly=$onlyPanelist&isWebViewBased=$isWebBased&sdkVersion=$sdkVersion&appVersion=$appVersion"
val metaCookie = CookieHandler.createHttpCookie(TagStringsAndValues.SIFO_META_COOKIE_NAME, cookieValue)
CookieHandler.setupPanelistCookies(listOf(metaCookie))
if (paramsAreValid(cpID, applicationName)) {
Expand All @@ -57,9 +59,11 @@ internal class TSMobileAnalyticsBackend : TSMobileAnalytics {

initTags(activity, cpID!!, applicationName!!, onlyPanelist)

sendAppStartEvent()

PanelistHandler.syncCookies(activity, activity) {
refreshCookiesAndKeys(activity,onlyPanelist)
frameworkInstance!!.dataRequestHandler.setStateReady()
frameworkInstance?.dataRequestHandler?.setStateReady()
}
} else {
log("Mobile Application Tagging Framework already initialized")
Expand All @@ -73,13 +77,16 @@ internal class TSMobileAnalyticsBackend : TSMobileAnalytics {
private fun refreshCookiesAndKeys(activity: ComponentActivity, onlyPanelist: Boolean) {
val isInstalled = activity.isPackageInstalled(TagStringsAndValues.SIFO_PANELIST_PACKAGE_NAME_V2)
if (!isInstalled) {
//TODO: 2021-09-02 Do we need logFatalError when onlyPanelist ==true?
if (onlyPanelist && !isInstalled) {
frameworkInstance = null
fatalError("To track panelists only you need to have the internet app installed")
}
//No need to refresh the cookies since there is no panelist app to get the cookies from
return
}
log("Refreshing panelist keys(Cookies)")
val cookies = PanelistHandler.getCookies(activity, activity)
frameworkInstance!!.dataRequestHandler.apply {
frameworkInstance?.dataRequestHandler?.apply {
if (cookies != null) {
refreshCookies(cookies)
} else {
Expand Down Expand Up @@ -126,6 +133,18 @@ internal class TSMobileAnalyticsBackend : TSMobileAnalytics {
return true
}

/**
* Sends the app start event
*/
private fun sendAppStartEvent() {
if (frameworkInstance ==null) {
log("Cannot create app start event")
return
}

frameworkInstance?.sendTag(SIFO_APP_START_EVENT_CATEGORY)
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package se.kantarsifo.mobileanalytics.framework
import android.annotation.SuppressLint
import android.content.Context
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Build.VERSION
import android.os.Build.VERSION_CODES
import android.provider.Settings.Secure
Expand All @@ -16,6 +17,7 @@ import org.json.JSONObject
import se.kantarsifo.mobileanalytics.framework.Logger.error
import se.kantarsifo.mobileanalytics.framework.Logger.fatalError
import se.kantarsifo.mobileanalytics.framework.Logger.log
import se.kantarsifo.mobileanalytics.framework.Utils.getApplicationVersion
import java.io.UnsupportedEncodingException
import java.net.CookieStore
import java.net.HttpCookie
Expand Down Expand Up @@ -54,7 +56,7 @@ internal class TagHandler(
init {
generateEuId(context)
cookies?.let { initCookies(it) }
getApplicationVersion(context)
applicationVersion = context.getApplicationVersion()
}

fun refresh(panelistKey: String?) {
Expand Down Expand Up @@ -86,6 +88,16 @@ internal class TagHandler(
.replace("{appNameValue}", type) // This seems like a mistake but the client confirmed they want it like this.
.replace("{appRefValue}", encodedRef)

applicationVersion?.apply {
val encodedVersion = urlEncode(trim())
val uri = Uri.parse(url)
.buildUpon()
.appendQueryParameter("appVersion",encodedVersion)
.build()
url = uri.toString()
}


}
return url
}
Expand Down Expand Up @@ -128,15 +140,6 @@ internal class TagHandler(
}
}

private fun getApplicationVersion(context: Context) {
try {
applicationVersion =
context.packageManager.getPackageInfo(context.packageName, 0).versionName
} catch (e: Exception) {
error("Failed to retrieve application version, will not set be set in request header")
}
}

private fun initCookies(cookieList: List<HttpCookie>) {
val setupPanelListCookies = SetupPanelListCookies(context, cookieList)
setupPanelListCookies.run()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,9 @@ object TagStringsAndValues {
*/
internal const val SIFO_META_COOKIE_NAME = "sifo_config"

/**
* app start event category name
*/
internal const val SIFO_APP_START_EVENT_CATEGORY = "appstart"

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,14 @@ object Utils {
false
}
}

fun Context.getApplicationVersion(): String? {
return try {
packageManager.getPackageInfo(packageName, 0).versionName
} catch (e: Exception) {
Logger.error("Failed to retrieve application version, will not set be set in request header")
null
}
}

}

0 comments on commit e1a2af0

Please sign in to comment.