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

Passing device Id upon initialization #36

Open
clarencedesmarais opened this issue Sep 28, 2020 · 4 comments
Open

Passing device Id upon initialization #36

clarencedesmarais opened this issue Sep 28, 2020 · 4 comments
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@clarencedesmarais
Copy link

Summary

Hey,

We are currently using the node SDK for amplitude in one of our backends. It seems that in order to log an event to amplitude, the device id or user id is required as specified here:
https://developers.amplitude.com/docs/http-api-v2#properties-1

Currently our backend does not track our users. We wanted to pass a variable for the device_id such as : service_server which would just be the name of our backend.

const amplitude = init(AMPLITUDE_TOKEN, { device_id: AMPLITUDE_DEVICE_ID });

Motivations

This would allow us to avoid passing the deviceId every time we want to log an event, which is not ideal since it is always the same anyways.

Don't hesitate to let me know if there is a better way of doing this. I'm new to using amplitude :)

@kelvin-lu kelvin-lu added enhancement New feature or request good first issue Good for newcomers labels Sep 28, 2020
@kelvin-lu
Copy link
Contributor

👋 hello @clarencedesmarais!

I think this would be a good idea - but I think it might help to not stop at device_id - we could provide a lot of help on filling in base values for user_id, language, platform, etc.. by default. We could provide a fallbackEventFields: Shape<Event> to fill in any missing values or a preprocessEvent: (event: Event) => ... option for users to fill in default values before they go in. Thoughts?

@clarencedesmarais
Copy link
Author

clarencedesmarais commented Sep 28, 2020

Yes, I agree with allowing more parameters within the init function. After a bit of research, it seems that the Javascript SDK's init function does not allow the deviceId to be passed in. I'm wondering why that would be.

I think the parameter format for the init function could be the same as in : https://amplitude.github.io/Amplitude-JavaScript/#amplitudeclientinit where options are https://github.com/amplitude/Amplitude-JavaScript/blob/master/src/options.js#L14

I would like to be able to pass deviceId in options as well, but maybe there is a reason that they don't allow to pass it. If that's ok, maybe we can add it inside the options as well.

Our use case is one where we actually don't track users or any device. We just want to send events from a backend to amplitude.

I've seen this method too : https://amplitude.github.io/Amplitude-JavaScript/#amplitudeclientsetdeviceid
But they specifically mention in the docs to avoid using this if we don't have a system in place for tracking device ids, which we don't, so I am a bit unsure.

If we don't track our users, would it make sense to name the deviceId simply the name of the backend that sends those events ?

Thanks.

@kelvin-lu
Copy link
Contributor

Hi @clarencedesmarais !

Apologies for the late response.

To start,

What happens if I log an event in Amplitude without a device ID?

We'll attach a UUID as the device ID - so there's no worry that there's no device ID

After a bit of research, it seems that the Javascript SDK's init function does not allow the deviceId to be passed in. I'm wondering why that would be.

it's not very well documented and a general thing we're tackling right now to improve our docs - you can pass it in in the options actually:

amplitude.getInstance().init("YOUR_API_KEY", { deviceId: "YOUR_DEVICE_ID" });

I've seen this method too : https://amplitude.github.io/Amplitude-JavaScript/#amplitudeclientsetdeviceid
But they specifically mention in the docs to avoid using this if we don't have a system in place for tracking device ids, which we don't, so I am a bit unsure.

This is specific for the use case for the JS SDK, which is specific to client-side implementations. It's really important for client SDK's to keep track of a user's identity so that they can be connected in user streams within amplitude - setDeviceId is dangerous because it effectively resets the ID to which the events are being attributed.

for ex. if I started logging at device_id_one and then set the device Id to device_id_two new events will no longer appear in the user stream view for device_id_one and some customers have been confused around this behavior, but this is important for use cases where the "identity" of the person sending events can change in the same browser.

We will likely add this as an option soon of making it easier to add fields/device id in the node SDK! Let me know if I missed anything/should clarify anything above

@axelvaindal
Copy link

Hello @kelvin-lu, sorry for the annoyance.

I'm struggling as well with the combination of deviceId/userId.
I'm trying to log events sent by my backend, but I'm not sure I understand your explanation.
I'm not currently setting anything to the deviceId and just setting userId when it's known (which is not always the case, as the backend can be consumed anonymously).

client.logEvent({
event_type: "Web:Server:Some:Event",
user_id: uid,
event_properties: {
    // some properties here
    }
});

What will happen in the case uid is undefined?
From my previous experience, it felt like Amplitude didn't log the event.

How can I update the previous code to ensure Amplitude logs the event, even if the user making the request is unknown?
Thanks for your help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

3 participants