Skip to content

Commit

Permalink
Merge pull request #99 from Parsely/issue/parsely_tracker_kotlin_migr…
Browse files Browse the repository at this point in the history
…ation

Migrate `ParselyTracker` from Java to Kotlin
  • Loading branch information
wzieba authored Jan 17, 2024
2 parents 9cbf080 + c2cb617 commit d4e225d
Show file tree
Hide file tree
Showing 13 changed files with 496 additions and 489 deletions.
5 changes: 1 addition & 4 deletions example/src/main/java/com/example/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@ protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);

// initialize the Parsely tracker with your site id and the current Context
ParselyTracker.sharedInstance("example.com", 30, this);

// Set debugging to true so we don't actually send things to Parse.ly
ParselyTracker.sharedInstance().setDebug(true);
ParselyTracker.sharedInstance("example.com", 30, this, true);

final TextView intervalView = (TextView) findViewById(R.id.interval);

Expand Down
5 changes: 5 additions & 0 deletions parsely/build.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import org.jetbrains.kotlin.gradle.dsl.ExplicitApiMode
plugins {
id 'com.android.library'
id 'org.jetbrains.kotlin.android'
Expand Down Expand Up @@ -57,6 +58,10 @@ android {
}
}
}

kotlin {
explicitApi(ExplicitApiMode.Strict)
}
}

dependencies {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class FunctionalTests {
parselyTracker = initializeTracker(activity)

repeat(51) {
parselyTracker.trackPageview("url", null, null, null)
parselyTracker.trackPageview("url")
}
}

Expand Down Expand Up @@ -93,13 +93,13 @@ class FunctionalTests {
server.enqueue(MockResponse().setResponseCode(200))
parselyTracker = initializeTracker(activity)

parselyTracker.trackPageview("url", null, null, null)
parselyTracker.trackPageview("url")
}

Thread.sleep((defaultFlushInterval / 2).inWholeMilliseconds)

scenario.onActivity {
parselyTracker.trackPageview("url", null, null, null)
parselyTracker.trackPageview("url")
}

Thread.sleep((defaultFlushInterval / 2).inWholeMilliseconds)
Expand All @@ -108,7 +108,7 @@ class FunctionalTests {
assertThat(firstRequestPayload!!["events"]).hasSize(2)

scenario.onActivity {
parselyTracker.trackPageview("url", null, null, null)
parselyTracker.trackPageview("url")
}

Thread.sleep(defaultFlushInterval.inWholeMilliseconds)
Expand Down Expand Up @@ -138,7 +138,7 @@ class FunctionalTests {
parselyTracker = initializeTracker(activity, flushInterval = 1.hours)

repeat(20) {
parselyTracker.trackPageview("url", null, null, null)
parselyTracker.trackPageview("url")
}
}

Expand Down Expand Up @@ -172,7 +172,7 @@ class FunctionalTests {
parselyTracker = initializeTracker(activity)

repeat(eventsToSend) {
parselyTracker.trackPageview("url", null, null, null)
parselyTracker.trackPageview("url")
}
}

Expand Down Expand Up @@ -221,8 +221,8 @@ class FunctionalTests {

// when
startTimestamp = System.currentTimeMillis().milliseconds
parselyTracker.trackPageview("url", null, null, null)
parselyTracker.startEngagement(engagementUrl, null)
parselyTracker.trackPageview("url")
parselyTracker.startEngagement(engagementUrl)
}

Thread.sleep((firstInterval + secondInterval + pauseInterval).inWholeMilliseconds)
Expand Down Expand Up @@ -315,7 +315,7 @@ class FunctionalTests {
activity: Activity,
flushInterval: Duration = defaultFlushInterval
): ParselyTracker {
val field: Field = ParselyTracker::class.java.getDeclaredField("ROOT_URL")
val field: Field = ParselyTrackerInternal::class.java.getDeclaredField("ROOT_URL")
field.isAccessible = true
field.set(this, url)
return ParselyTracker.sharedInstance(
Expand Down
4 changes: 2 additions & 2 deletions parsely/src/main/java/com/parsely/parselyandroid/Clock.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import java.util.TimeZone
import kotlin.time.Duration
import kotlin.time.Duration.Companion.milliseconds

open class Clock {
open val now: Duration
internal open class Clock {
internal open val now: Duration
get() = Calendar.getInstance(TimeZone.getTimeZone("UTC")).timeInMillis.milliseconds
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import kotlinx.coroutines.launch
* 2. Progressive backoff for long engagements to save data.
*/
internal class EngagementManager(
private val parselyTracker: ParselyTracker,
private val eventQueuer: EventQueuer,
private var latestDelayMillis: Long,
private val baseEvent: Map<String, Any>,
private val intervalCalculator: HeartbeatIntervalCalculator,
Expand Down Expand Up @@ -76,7 +76,7 @@ internal class EngagementManager(
totalTime += inc
event["inc"] = inc / 1000
event["tt"] = totalTime
parselyTracker.enqueueEvent(event)
eventQueuer.enqueueEvent(event)
}

val intervalMillis: Double
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.parsely.parselyandroid

internal interface EventQueuer {
fun enqueueEvent(event: Map<String, Any>)
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ internal class FlushQueue(
log("Sending request with %d events", eventsToSend.size)
val jsonPayload = toParselyEventsPayload(eventsToSend)
log("POST Data %s", jsonPayload)
log("Requested %s", ParselyTracker.ROOT_URL)
log("Requested %s", ParselyTrackerInternal.ROOT_URL)
restClient.send(jsonPayload)
.fold(
onSuccess = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import java.util.Calendar
* internet (i.e. app-only content) or if the customer is using an "in-pixel" integration.
* Otherwise, metadata will be gathered by Parse.ly's crawling infrastructure.
*/
open class ParselyMetadata
public open class ParselyMetadata
/**
* Create a new ParselyMetadata object.
*
Expand All @@ -36,7 +36,7 @@ open class ParselyMetadata
*
* @return a Map object representing the metadata.
*/
open fun toMap(): Map<String, Any?> {
internal open fun toMap(): Map<String, Any?> {
val output: MutableMap<String, Any?> = HashMap()
if (authors != null) {
output["authors"] = authors
Expand Down
Loading

0 comments on commit d4e225d

Please sign in to comment.