Skip to content
Merged
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
82 changes: 82 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
name: Build

on:
pull_request:
branches: ['**']

jobs:
build:
name: Lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6

- uses: actions/setup-node@v6
with:
node-version: 20
cache: 'npm'

- name: Install dependencies
run: npm ci

- name: Lint
run: npm run lint

verify-web:
name: Verify Web
runs-on: ubuntu-latest
needs: build
steps:
- uses: actions/checkout@v6

- uses: actions/setup-node@v6
with:
node-version: 20
cache: 'npm'

- name: Install dependencies
run: npm ci

- name: Verify Web
run: npm run verify:web

verify-android:
name: Verify Android
runs-on: ubuntu-latest
needs: build
steps:
- uses: actions/checkout@v6

- uses: actions/setup-node@v6
with:
node-version: 20
cache: 'npm'

- uses: actions/setup-java@v4
with:
distribution: 'temurin'
java-version: '21'

- name: Install dependencies
run: npm ci

- name: Verify Android
run: npm run verify:android

verify-ios:
name: Verify iOS
runs-on: macos-latest
needs: build
steps:
- uses: actions/checkout@v6

- uses: actions/setup-node@v6
with:
node-version: 20
cache: 'npm'

- name: Install dependencies
run: npm ci

- name: Verify iOS
run: npm run verify:ios
5 changes: 1 addition & 4 deletions android/src/main/java/com/joinflux/flux/segment/Segment.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
package com.joinflux.flux.segment;

import android.util.Log;

import com.getcapacitor.JSObject;
import com.segment.analytics.Analytics;
import com.segment.analytics.Options;
import com.segment.analytics.Properties;
import com.segment.analytics.Traits;

import org.json.JSONException;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.json.JSONException;

public class Segment {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.joinflux.flux.segment;

import android.content.Context;

import com.getcapacitor.JSObject;
import com.getcapacitor.Plugin;
import com.getcapacitor.PluginCall;
Expand All @@ -18,7 +17,7 @@ public class SegmentPlugin extends Plugin {

@PluginMethod
public void initialize(PluginCall call) {
synchronized(implementation) {
synchronized (implementation) {
// No-op
if (initialized) {
call.resolve();
Expand Down
12 changes: 6 additions & 6 deletions ios/Plugin/Segment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ import Segment
@objc public class Segment: NSObject {
@objc public func initialize(key: String, trackLifecycle: Bool) {
let config = AnalyticsConfiguration.init(writeKey: key)
config.trackApplicationLifecycleEvents = trackLifecycle;
config.trackApplicationLifecycleEvents = trackLifecycle

Analytics.setup(with: config)
print("CapacitorSegment: initialized")
}
@objc public func identify(userId: String, traits: Dictionary<String, Any>) {

@objc public func identify(userId: String, traits: [String: Any]) {
Analytics.shared().identify(userId, traits: traits)
return
}

@objc public func track(eventName: String, properties: Dictionary<String, Any>, options: Dictionary<String, Any>) {
@objc public func track(eventName: String, properties: [String: Any], options: [String: Any]) {
Analytics.shared().track(eventName, properties: properties, options: options)
return
}
Expand All @@ -24,7 +24,7 @@ import Segment
Analytics.shared().screen(pathname)
return
}

@objc func reset() {
Analytics.shared().reset()
return
Expand Down
24 changes: 12 additions & 12 deletions ios/Plugin/SegmentPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class SegmentPlugin: CAPPlugin {
private let implementation = Segment()

@objc func initialize(_ call: CAPPluginCall) {
if (initialized == true) {
if initialized == true {
call.resolve()
return
}
Expand All @@ -24,58 +24,58 @@ public class SegmentPlugin: CAPPlugin {
initialized = true
call.resolve()
}

@objc func identify(_ call: CAPPluginCall) {
if (initialized != true) {
if initialized != true {
call.reject("Segment is not initialized")
return
}
guard let userId = call.getString("userId") else {
call.reject("User ID is required for 'identify' but not supplied")
return
}

let traits: Dictionary = call.getObject("traits") ?? [:]
implementation.identify(userId: userId, traits: traits)
call.resolve()
}

@objc func track(_ call: CAPPluginCall) {
if (initialized != true) {
if initialized != true {
call.reject("Segment is not initialized")
return
}
guard let eventName = call.getString("eventName") else {
call.reject("Event name is not supplied")
return
}

let properties: Dictionary = call.getObject("properties") ?? [:]
let options: Dictionary = call.getObject("options") ?? [:]
implementation.track(eventName: eventName, properties: properties, options: options)
call.resolve()
}

@objc func page(_ call: CAPPluginCall) {
if (initialized != true) {
if initialized != true {
call.reject("Segment is not initialized")
return
}
guard let pathname = call.getString("pathname") else {
call.reject("Pathname was not supplied")
return
}

implementation.page(pathname: pathname)
call.resolve()
}

@objc func reset(_ call: CAPPluginCall) {
if (initialized != true) {
if initialized != true {
call.reject("Segment is not initialized")
return
}

implementation.reset()
call.resolve()
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"verify:android": "cd android && ./gradlew clean build test && cd ..",
"verify:web": "npm run build",
"lint": "npm run eslint && npm run prettier -- --check && npm run swiftlint -- lint",
"fmt": "npm run eslint -- --fix && npm run prettier -- --write && npm run swiftlint -- autocorrect --format",
"fmt": "npm run eslint -- --fix && npm run prettier -- --write && npm run swiftlint -- --autocorrect --format",
"eslint": "eslint . --ext ts",
"prettier": "prettier \"**/*.{css,html,ts,js,java}\" --plugin=prettier-plugin-java",
"swiftlint": "node-swiftlint",
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { registerPlugin } from '@capacitor/core';
import type { SegmentPlugin } from './definitions';

const Segment = registerPlugin<SegmentPlugin>('Segment', {
web: () => import('./web').then(m => new m.SegmentWeb()),
web: () => import('./web').then((m) => new m.SegmentWeb()),
});

export * from './definitions';
Expand Down
15 changes: 3 additions & 12 deletions src/web.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
import { WebPlugin } from '@capacitor/core';

import type {
SegmentPlugin,
IdentifyOptions,
InitializeOptions,
PageOptions,
TrackOptions,
} from './definitions';
import type { SegmentPlugin, IdentifyOptions, InitializeOptions, PageOptions, TrackOptions } from './definitions';

const getSegmentScript = (
key: string,
Expand All @@ -27,10 +21,7 @@ export class SegmentWeb extends WebPlugin implements SegmentPlugin {

async identify(options: IdentifyOptions): Promise<void> {
if (!window.analytics) return Promise.reject('Segment is not initialized');
if (!options.userId)
return Promise.reject(
"User ID is required for 'identify' but not supplied",
);
if (!options.userId) return Promise.reject("User ID is required for 'identify' but not supplied");
window.analytics.identify(options.userId, options.traits, options.options);
}

Expand All @@ -57,7 +48,7 @@ export class SegmentWeb extends WebPlugin implements SegmentPlugin {
* @param src - source of the script
*/
private loadScript(id: string, script: string): Promise<any> {
return new Promise(resolve => {
return new Promise((resolve) => {
if (document.getElementById(id)) {
resolve(null);
} else {
Expand Down