Skip to content
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

v1.0.2 #4

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.0.2

* Added feature to customize HTTP requests

## 1.0.1
* PianoConsents has been moved to the piano_consents package
* Added feature to specify property type via forceType argument
Expand Down
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ Initialize PianoAnalytics with your site and collect domain in your application
storageLifetimeVisitor: 395,
visitorStorageMode: VisitorStorageMode.fixed,
ignoreLimitedAdvertisingTracking: true,
visitorId: "WEB-192203AJ"
visitorId: "WEB-192203AJ",
headers: {"X-Request-Id": "123456789"},
query: {"request_id": "123456789"}
);

@override
Expand All @@ -59,6 +61,18 @@ Initialize PianoAnalytics with your site and collect domain in your application
}
```

### Set HTTP parameters

Headers:
```dart
await _pianoAnalytics.setHeader(key: "X-User-Id", value: "WEB-192203AJ")
```

Query string parameters:
```dart
await _pianoAnalytics.setQuery(key: "user_id", value: "WEB-192203AJ")
```

### Send events
```dart
await _pianoAnalytics.sendEvents(events: [
Expand Down
2 changes: 2 additions & 0 deletions android/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ gradle
/build
/captures
.cxx

property*
14 changes: 7 additions & 7 deletions android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
group = "io.piano.flutter.piano_analytics"
version = "1.0.1"
version = "1.0.2"

buildscript {
ext.kotlin_version = "1.9.0"
ext.kotlin_version = "2.1.0"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better to use 2.0.21, as android library does

repositories {
google()
mavenCentral()
}

dependencies {
classpath("com.android.tools.build:gradle:7.3.0")
classpath("com.android.tools.build:gradle:8.7.3")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about 8.8.0?

classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version")
}
}
Expand All @@ -25,7 +25,7 @@ apply plugin: "com.android.library"
apply plugin: "kotlin-android"

dependencies {
api("io.piano.android:analytics:3.4.1")
api("io.piano.android:analytics:3.5.0")
}

android {
Expand All @@ -36,12 +36,12 @@ android {
compileSdk = 34

compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}

kotlinOptions {
jvmTarget = JavaVersion.VERSION_1_8
jvmTarget = JavaVersion.VERSION_11
}

sourceSets {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import io.flutter.plugin.common.MethodChannel.Result
import io.flutter.plugin.common.StandardMessageCodec
import io.flutter.plugin.common.StandardMethodCodec
import io.piano.android.analytics.Configuration
import io.piano.android.analytics.CustomHttpDataProvider
import io.piano.android.analytics.PianoAnalytics
import io.piano.android.analytics.model.Event
import io.piano.android.analytics.model.PrivacyMode
Expand All @@ -37,7 +38,18 @@ private class Codec : StandardMessageCodec() {
}
}

class PianoAnalyticsPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
class HttpDataProvider : CustomHttpDataProvider {

val headers = mutableMapOf<String, String>()
var parameters = mutableMapOf<String, String>()

override fun headers() = headers
override fun parameters() = parameters
}

class PianoAnalyticsPlugin(
private val httpDataProvider: HttpDataProvider = HttpDataProvider()
) : FlutterPlugin, MethodCallHandler, ActivityAware {

private lateinit var channel: MethodChannel
private lateinit var visitorIDType: VisitorIDType
Expand Down Expand Up @@ -78,6 +90,8 @@ class PianoAnalyticsPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
val methodResult: Any? = when (call.method) {
// Main
"init" -> handleInit(call)
"setHeader" -> handleSetHeader(call)
"setQuery" -> handleSetQuery(call)
"send" -> handleSend(call)
// User
"getUser" -> handleGetUser()
Expand All @@ -103,6 +117,12 @@ class PianoAnalyticsPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {

private fun handleInit(call: MethodCall) {
visitorIDType = getVisitorIDType(call.arg("visitorIDType"))
call.argument<Map<String, String>>("headers")?.let {
httpDataProvider.headers.putAll(it)
}
call.argument<Map<String, String>>("query")?.let {
httpDataProvider.parameters.putAll(it)
}
val pianoAnalytics = PianoAnalytics.init(
context = context.get() ?: error("Activity not attached"),
configuration = Configuration.Builder(
Expand All @@ -113,9 +133,10 @@ class PianoAnalyticsPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
?: Configuration.DEFAULT_VISITOR_STORAGE_LIFETIME,
visitorStorageMode = getVisitorStorageMode(call.argument("visitorStorageMode")),
ignoreLimitedAdTracking = call.argument<Boolean>("ignoreLimitedAdvertisingTracking")
?: false
?: false,
).build(),
pianoConsents = getPianoConsents()
pianoConsents = getPianoConsents(),
customHttpDataProvider = httpDataProvider
)

if (visitorIDType == VisitorIDType.CUSTOM) {
Expand All @@ -125,6 +146,24 @@ class PianoAnalyticsPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
}
}

private fun handleSetHeader(call: MethodCall) {
val value = call.argument<String>("value")
if (value != null) {
httpDataProvider.headers[call.arg("key")] = value
} else {
httpDataProvider.headers.remove(call.arg("key"))
}
}

private fun handleSetQuery(call: MethodCall) {
val value = call.argument<String>("value")
if (value != null) {
httpDataProvider.parameters[call.arg("key")] = value
} else {
httpDataProvider.parameters.remove(call.arg("key"))
}
}

private fun handleSend(call: MethodCall) {
val events = call.arg<List<Map<String, Any>>>("events").map {
val name = it["name"] as? String ?: error("Undefined event name")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import java.util.Date
import kotlin.test.BeforeTest
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNull

internal class PianoAnalyticsPluginTest : BasePluginTest() {

Expand All @@ -31,25 +32,30 @@ internal class PianoAnalyticsPluginTest : BasePluginTest() {
@Test
fun `Check init`() {
val pianoAnalytics: PianoAnalytics = mockk()
every { PianoAnalytics.Companion.init(any(), any(), any(), any()) } returns pianoAnalytics
every { PianoAnalytics.Companion.init(any(), any(), any(), any(), any()) } returns pianoAnalytics

val customVisitorIdSlot = slot<String>()
every { pianoAnalytics.customVisitorId = capture(customVisitorIdSlot) } answers {}

val httpDataProvider = HttpDataProvider()
call(
"init", mapOf(
"init",
mapOf(
"site" to 123456789,
"collectDomain" to "xxxxxxx.pa-cd.com",
"visitorIDType" to "CUSTOM",
"visitorStorageLifetime" to 395,
"visitorStorageMode" to "fixed",
"ignoreLimitedAdvertisingTracking" to true,
"visitorId" to "WEB-192203AJ"
)
)
"visitorId" to "WEB-192203AJ",
"headers" to mapOf("X-Request-ID" to "123456789"),
"query" to mapOf("request_id" to "123456789")
),
null
) { PianoAnalyticsPlugin(httpDataProvider) }

val slot = slot<Configuration>()
verify { PianoAnalytics.init(any(), capture(slot), any(), any()) }
verify { PianoAnalytics.init(any(), capture(slot), any(), any(), any()) }

val configuration = slot.captured
assertEquals(123456789, configuration.site)
Expand All @@ -58,7 +64,8 @@ internal class PianoAnalyticsPluginTest : BasePluginTest() {
assertEquals(395, configuration.visitorStorageLifetime)
assertEquals(VisitorStorageMode.FIXED, configuration.visitorStorageMode)
assertEquals(true, configuration.ignoreLimitedAdTracking)
assertEquals("WEB-192203AJ", customVisitorIdSlot.captured)
assertEquals("123456789", httpDataProvider.headers["X-Request-ID"])
assertEquals("123456789", httpDataProvider.parameters["request_id"])
}

@Test
Expand Down Expand Up @@ -155,6 +162,56 @@ internal class PianoAnalyticsPluginTest : BasePluginTest() {
)
}

@Test
fun `Check setHeader`() {
val httpDataProvider = HttpDataProvider()
call(
"setHeader",
mapOf(
"key" to "X-User-Id",
"value" to "WEB-192203AJ"
),
null
) { PianoAnalyticsPlugin(httpDataProvider) }

assertEquals("WEB-192203AJ", httpDataProvider.headers["X-User-Id"])

call(
"setHeader",
mapOf(
"key" to "X-User-Id"
),
null
) { PianoAnalyticsPlugin(httpDataProvider) }

assertNull(httpDataProvider.headers["X-User-Id"])
}

@Test
fun `Check setQuery`() {
val httpDataProvider = HttpDataProvider()
call(
"setQuery",
mapOf(
"key" to "user_id",
"value" to "WEB-192203AJ"
),
null
) { PianoAnalyticsPlugin(httpDataProvider) }

assertEquals("WEB-192203AJ", httpDataProvider.parameters["user_id"])

call(
"setQuery",
mapOf(
"key" to "user_id"
),
null
) { PianoAnalyticsPlugin(httpDataProvider) }

assertNull(httpDataProvider.parameters["user_id"])
}

@Test
fun `Check getUser`() {
val pianoAnalytics: PianoAnalytics = mockk()
Expand Down Expand Up @@ -210,7 +267,7 @@ internal class PianoAnalyticsPluginTest : BasePluginTest() {

@Test
fun `Check getVisitorId`() {
every { PianoAnalytics.Companion.init(any(), any(), any(), any()) } returns mockk()
every { PianoAnalytics.Companion.init(any(), any(), any(), any(), any()) } returns mockk()

val pianoAnalytics: PianoAnalytics = mockk()
every { PianoAnalytics.Companion.getInstance() } returns pianoAnalytics
Expand Down
2 changes: 2 additions & 0 deletions example/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
*.swp
.DS_Store
.atom/
.build/
.buildlog/
.history
.svn/
.swiftpm/
migrate_working_dir/

# IntelliJ related
Expand Down
3 changes: 3 additions & 0 deletions example/android/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ GeneratedPluginRegistrant.java
key.properties
**/*.keystore
**/*.jks

app/property*
app/.cxx
12 changes: 8 additions & 4 deletions example/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,15 @@ if (flutterVersionName == null) {
android {
namespace = "io.piano.piano_analytics_example"
compileSdk = flutter.compileSdkVersion
ndkVersion = flutter.ndkVersion


compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}

kotlinOptions {
jvmTarget = JavaVersion.VERSION_11
}

defaultConfig {
Expand All @@ -55,4 +59,4 @@ android {

flutter {
source = "../.."
}
}
2 changes: 1 addition & 1 deletion example/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ allprojects {

rootProject.layout.buildDirectory = "../build"
subprojects {
project.layout.buildDirectory = "${rootProject.buildDir}/${project.name}"
project.layout.buildDirectory = "${rootProject.layout.buildDirectory.get()}/${project.name}"
}
subprojects {
project.evaluationDependsOn(":app")
Expand Down
4 changes: 2 additions & 2 deletions example/android/settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ pluginManagement {

plugins {
id "dev.flutter.flutter-plugin-loader" version "1.0.0"
id "com.android.application" version '7.4.2' apply false
id "org.jetbrains.kotlin.android" version "1.9.0" apply false
id "com.android.application" version '8.7.3' apply false
id "org.jetbrains.kotlin.android" version "2.1.0" apply false
}

include ":app"
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
enableGPUValidationMode = "1"
allowLocationSimulation = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
Expand Down

This file was deleted.

This file was deleted.

Loading