Skip to content

Node Issue #309 #2592

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

Merged
merged 6 commits into from
Mar 16, 2022
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
15 changes: 7 additions & 8 deletions src/connections/sources/catalog/libraries/server/java/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Add to `pom.xml`:
or if you're using Gradle:

```bash
compile 'com.segment.analytics.java:analytics:+'
implementation 'com.segment.analytics.java:analytics:+'
```

### Initialize the SDK
Expand Down Expand Up @@ -74,14 +74,13 @@ We recommend calling `identify` a single time when the user's account is first c
Example `identify` call:

```java
Map<String, String> map = new HashMap();
map.put("name", "Michael Bolton");
map.put("email", "mbolton@example.com");

analytics.enqueue(IdentifyMessage.builder()
.userId("f4ca124298")
.traits(ImmutableMap.builder()
.put("name", "Michael Bolton")
.put("email", "mbolton@example.com")
.build()
)
);
.userId("f4ca124298")
.traits(map));
```

This call is identifying Michael by his unique User ID (the one you know him by in your database) and labeling him with `name` and `email` traits.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Here's what it would look like with Maven:
*or if you're using Gradle:*

```bash
compile 'com.segment.analytics.java:analytics:+'
implementation 'com.segment.analytics.java:analytics:+'
```

## Step 3: Initialize the SDK
Expand Down Expand Up @@ -71,14 +71,13 @@ The `identify` message is how you tell Segment who the current user is. It inclu
Here's what a basic call to `identify` a user might look like:

```java
Map<String, String> map = new HashMap();
map.put("name", "Michael Bolton");
map.put("email", "mbolton@example.com");

analytics.enqueue(IdentifyMessage.builder()
.userId("f4ca124298")
.traits(ImmutableMap.builder()
.put("name", "Michael Bolton")
.put("email", "mbolton@example.com")
.build()
)
);
.userId("f4ca124298")
.traits(map));
```

**Note:** The enqueue method takes a `MessageBuilder` instance and not a `Message` instance directly. This is to allow you to use a `MessageTransformer` that applies to all incoming messages and transform or add data. <!-- LR: Can't find that we ever had a doc about this. Read more about it in the [transformer reference docs](/docs/connections/sources/catalog/libraries/server/java#transformer).-->
Expand Down
78 changes: 78 additions & 0 deletions src/connections/sources/catalog/libraries/server/node/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,84 @@ analytics.flush(function(err, batch){
});
```

## Long running process

You should call `client.track(...)` and know that events will be queued and eventually sent to Segment. To prevent losing messages, be sure to capture any interruption (for example, a server restart) and call flush to know of and delay the process shutdown.

```js
import { randomUUID } from 'crypto';
import Analytics from 'analytics-node'

const WRITE_KEY = '...';

const analytics = new Analytics(WRITE_KEY, { flushAt: 10 });

analytics.track({
anonymousId: randomUUID(),
event: 'Test event',
properties: {
name: 'Test event',
timestamp: new Date()
}
});

const exitGracefully = async (code) => {
console.log('Flushing events');
await analytics.flush(function(err, batch) {
console.log('Flushed, and now this program can exit!');
process.exit(code);
});
};

[
'beforeExit', 'uncaughtException', 'unhandledRejection',
'SIGHUP', 'SIGINT', 'SIGQUIT', 'SIGILL', 'SIGTRAP',
'SIGABRT','SIGBUS', 'SIGFPE', 'SIGUSR1', 'SIGSEGV',
'SIGUSR2', 'SIGTERM',
].forEach(evt => process.on(evt, exitGracefully));

function logEvery2Seconds(i) {
setTimeout(() => {
console.log('Infinite Loop Test n:', i);
logEvery2Seconds(++i);
}, 2000);
}

logEvery2Seconds(0);
```

## Short lived process

Short-lived functions have a predictably short and linear lifecycle, so use a queue big enough to hold all messages and then await flush to complete its work.


```js
import { randomUUID } from 'crypto';
import Analytics from 'analytics-node'


async function lambda()
{
const WRITE_KEY = '...';
const analytics = new Analytics(WRITE_KEY, { flushAt: 20 });
analytics.flushed = true;

analytics.track({
anonymousId: randomUUID(),
event: 'Test event',
properties: {
name: 'Test event',
timestamp: new Date()
}
});
await analytics.flush(function(err, batch) {
console.log('Flushed, and now this program can exit!');
});
}

lambda();
```


## Multiple Clients

Expand Down