0

Need Typescript for Segment-Analytics (Analytics.js) or need user module in "analytics-node"

Hello Segment team,
I'm trying to put isomorphic generic code that will allow my NodeJs server code as well as ReactNative client code to use SegmentIO tracking.

So, I used "analytics-node": "3.3.0", and "@types/analytics-node": "0.0.33",
Unfortunately, `analytics-node` package doesn't have user() methods exposed and hence I'm not able to use:

analytics.user().anonymousId();

So, I tried to use analytics.js "@segment/analytics.js-core": "3.7.2", "@types/segment-analytics": "0.0.28"
But,

  1. There are discrepancies in Segment Documentation and actual library (the init/load method doesn't accept options {flush, FlushAt})
  2. and the @type  package is not exporting the Analytics  object so not even able to import and use it properly.

So can you guys helpme with this?

Please let me know if I'm using incorrect combinations of actual libs and @type libs, and direct me to correct one.

Or, let me know a work around to use analytics-node for `anonymousId()` .

I look forward for your help.
Thanks.
-
Piyush

4replies Oldest first
  • Oldest first
  • Newest first
  • Active threads
  • Popular
  • Ideally it would be better to have entire user module in analytics-node.
    Because user().anonymousId() generates the uuid and sets  for current user as well.
    Currently I'm generating my own userid using uuid/v4. but then I've to pass that manually to my track()

    Reply Upvote
  • Hi Piyush,

    Thanks for reaching out! Anywhere you can send POST requests using at least vanilla JS, you can implement Segment. Strictly speaking, it's difficult for us to provide implementation support for Typescript because Segment's javascript library wouldn't work inside of a typescript file since it's written in normal javascript.
    You'd need to compile your .ts file to normal javascript before inserting Segment's library.
    Additionally, you might find this resource for implementing Segment with SPA's worth a review.
    For the anonymousId we don't have an equivalent method for server-side, so it's best to manually grab the anonymousId via this method and then pass it server-side. Hope this helps!

    Cheers,
    Yannick

    Reply Upvote
  • Hello Yannick,

    Thanks for responding and sharing the alternative.

    I believe I've already tried using "analytics.js". 
    My entire project is in Typescript and Typescript isn't allowing me to import javascript module as is.
    Also, there is one Typescript library available for it but "analytics" class is not exported. So, I couldn't use 

    analytics.user().anonymousId()

    Any future plans to have Typescript lib from Segment?
    I can wait for some time if that's possible.
    Please let me know and feel free to close this ticket.
    Cheers.

    Reply Upvote
  • Hi Piyush,
    Currently, a Typescript library from Segment is not on our Roadmap. However, I will bring up your request to the product team! :)
    Cheers,
    Yannick

    Reply Upvote