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

(android) add the ability to configure proxying http calls #12

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
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ npx cap sync
initialize(options: InitializeOptions) => any
```

| Param | Type |
| ------------- | ------------------------------------------------------------------------------------ |
| **`options`** | <code>{ key: string; trackLifecycle?: boolean; recordScreenViews?: boolean; }</code> |
| Param | Type |
| ------------- | -------------------------------------------------------------------------------------------------------- |
| **`options`** | <code>{ key: string; trackLifecycle?: boolean; recordScreenViews?: boolean; proxyHost?: string; }</code> |

**Returns:** <code>any</code>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,18 @@
import com.getcapacitor.annotation.CapacitorPlugin;

import android.content.Context;
import android.net.Uri;
import android.util.Log;
import com.segment.analytics.Analytics;
import com.segment.analytics.Analytics.Builder;
import com.segment.analytics.ConnectionFactory;
import com.segment.analytics.Options;
import com.segment.analytics.Properties;
import com.segment.analytics.Traits;

import java.io.IOException;
import java.net.HttpURLConnection;

@CapacitorPlugin(name = "CapacitorSegment")
public class CapacitorSegmentPlugin extends Plugin {
private boolean initialized = false;
Expand All @@ -32,7 +37,22 @@ public void initialize(PluginCall call) {
}

Context context = this.getContext();
Builder builder = new Analytics.Builder(context, key);

Builder builder;
String proxyHost = call.getString("proxyHost");

if (proxyHost == null) {
builder = new Analytics.Builder(context, key);
} else {
builder = new Analytics.Builder(context, key).connectionFactory(new ConnectionFactory() {
@Override
protected HttpURLConnection openConnection(String url) throws IOException {
String path = Uri.parse(url).getPath();
return super.openConnection(proxyHost + path);
}
});
}

Boolean trackLifecycle = call.getBoolean("trackLifecycle", false);
if (trackLifecycle) {
builder.trackApplicationLifecycleEvents().experimentalUseNewLifecycleMethods(false);
Expand Down
1 change: 1 addition & 0 deletions src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export type InitializeOptions = {
key: string;
trackLifecycle?: boolean;
recordScreenViews?: boolean;
proxyHost?: string;
};

export type Identity = { userId: string };
Expand Down
4 changes: 2 additions & 2 deletions src/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { WebPlugin } from '@capacitor/core';

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

const getSegmentScript = (key: string) => `!function(){var analytics=window.analytics=window.analytics||[];if(!analytics.initialize)if(analytics.invoked)window.console&&console.error&&console.error("Segment snippet included twice.");else{analytics.invoked=!0;analytics.methods=["trackSubmit","trackClick","trackLink","trackForm","pageview","identify","reset","group","track","ready","alias","debug","page","once","off","on","addSourceMiddleware","addIntegrationMiddleware","setAnonymousId","addDestinationMiddleware"];analytics.factory=function(e){return function(){var t=Array.prototype.slice.call(arguments);t.unshift(e);analytics.push(t);return analytics}};for(var e=0;e<analytics.methods.length;e++){var key=analytics.methods[e];analytics[key]=analytics.factory(key)}analytics.load=function(key,e){var t=document.createElement("script");t.type="text/javascript";t.async=!0;t.src="https://cdn.segment.com/analytics.js/v1/" + key + "/analytics.min.js";var n=document.getElementsByTagName("script")[0];n.parentNode.insertBefore(t,n);analytics._loadOptions=e};analytics._writeKey="${key}";analytics.SNIPPET_VERSION="4.13.2";
const getSegmentScript = (key: string, hostname?: string) => `!function(){var analytics=window.analytics=window.analytics||[];if(!analytics.initialize)if(analytics.invoked)window.console&&console.error&&console.error("Segment snippet included twice.");else{analytics.invoked=!0;analytics.methods=["trackSubmit","trackClick","trackLink","trackForm","pageview","identify","reset","group","track","ready","alias","debug","page","once","off","on","addSourceMiddleware","addIntegrationMiddleware","setAnonymousId","addDestinationMiddleware"];analytics.factory=function(e){return function(){var t=Array.prototype.slice.call(arguments);t.unshift(e);analytics.push(t);return analytics}};for(var e=0;e<analytics.methods.length;e++){var key=analytics.methods[e];analytics[key]=analytics.factory(key)}analytics.load=function(key,e){var t=document.createElement("script");t.type="text/javascript";t.async=!0;t.src="${hostname ? hostname : 'https://cdn.segment.com'}/analytics.js/v1/" + key + "/analytics.min.js";var n=document.getElementsByTagName("script")[0];n.parentNode.insertBefore(t,n);analytics._loadOptions=e};analytics._writeKey="${key}";analytics.SNIPPET_VERSION="4.13.2";
analytics.load("${key}");
analytics.page();
}}();`
Expand All @@ -17,7 +17,7 @@ extends WebPlugin
implements CapacitorSegmentPlugin {

async initialize(options: InitializeOptions): Promise<void> {
await this.loadScript('segment', getSegmentScript(options.key));
await this.loadScript('segment', getSegmentScript(options.key, options?.proxyHost));
}

async identify(options: IdentifyOptions): Promise<void> {
Expand Down