简体   繁体   English

如果用户没有带有 Next.js 应用程序的 cookie,则在客户端上从 SSR 设置分段 ajs_anonymous_id?

[英]Setting Segment ajs_anonymous_id from SSR on client if user doesn't have cookie with Next.js App?

Anyone set the ajs_anonymous_id in SSR (nextjs) if it doesn't currently exist on client?如果客户端上当前不存在 ajs_anonymous_id,是否有人在 SSR (nextjs) 中设置它?

I have a need to "read" the ajs_anonymous_id (Segment analytics) cookie during a SSR rendering in Next.Js, but of course there are instances when that cookie does not exist yet ie...person hasn't visited my site before and thus never go it... BUT, since i need in SSR side..... I was hoping there is a process I can "set it" on the server side so that I can use it, THEN have it on the client too... so..我需要在 Next.Js 中的 SSR 渲染期间“读取” ajs_anonymous_id(细分分析)cookie,但当然存在该 cookie 尚不存在的情况,即......人之前没有访问过我的网站并且因此永远不要 go 它......但是,因为我需要在 SSR 端......我希望有一个过程我可以在服务器端“设置”以便我可以使用它,然后将它放在客户端太……那么……

client visits page客户访问页面

Has ajs_anonymous_id cookie, cool, use it and do some display things....有 ajs_anonymous_id cookie,很酷,使用它并做一些显示的事情....

Does not have ajs_anonymous_id, I seed the ajs_anonymous_id (drop a cookie) and then do some display things.没有 ajs_anonymous_id,我播种 ajs_anonymous_id(放置一个 cookie)然后做一些显示的事情。

pages loads.. My analytics file (that loads on the font end thru a containe) sees there is an already ajs_anonymous_id cookie, cool.页面加载.. 我的分析文件(通过包含在字体末端加载)看到已经有一个 ajs_anonymous_id cookie,很酷。

Anyone have an example of this or how to achieve it?任何人都有这个例子或如何实现它?

yeah - there seems to be a package and method just for this.是的 - 似乎有一个 package 和方法就是为了这个。


import Analytics from 'analytics-node'

// if no anonymousId, send a randomly generated one
  // otherwise grab existing to include in call to segment
  let anonymousId
  if (cookies.ajs_anonymous_id) {
    anonymousId = cookies.ajs_anonymous_id
  } else {
    anonymousId = = uuid.v4()
    res.cookie('ajs_anonymous_id', anonymousId )
  }

for the full example, i'd refer to their docs: https://segment.com/docs/guides/how-to-guides/collect-pageviews-serverside/对于完整的例子,我会参考他们的文档: https://segment.com/docs/guides/how-to-guides/collect-pageviews-serverside/

For NextJS, I expect you have to move away from their server default implementation and build a wrapper to allow you to add this type of middleware.对于 NextJS,我希望您必须摆脱他们的服务器默认实现并构建一个包装器以允许您添加这种类型的中间件。

const express = require('express');
const bodyParser = require('body-parser');
const next = require('next');

const host = process.env.LOCAL_ADDRESS || '0.0.0.0';
const port = parseInt(process.env.NODE_PORT, 10) || 3000;
const dev = process.env.NODE_ENV !== 'production';
const app = next({ dev, dir: 'app' });
const handle = app.getRequestHandler();

app.prepare().then(() => {
    const server = express();

    server.use(bodyParser.urlencoded({ extended: false }));
    server.set('trust proxy', true);

    server.use(customMiddleware);

    server.all('*', (req, ...args) => {
        // Log the request only if we need to (we don't log all static asset requests)
        if (req.log) {
            req.log.info('incoming request');
        }

        return handle(req, ...args);
    });

    server.listen(port, host, (err) => {
        if (err) throw err;
        console.log(`> Ready on http://${host}:${port}`);
    });
});

then start the app with node app/server然后使用node app/server启动应用程序

Hope that helps to get you started!希望这有助于您入门!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM