Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.enable('trust proxy')
if (process.env.MATOMO_URL) {
console.log('Matomo Analytics activated');
app.use(matomo({
siteId: process.env.MATOMO_SITE,
matomoUrl: process.env.MATOMO_URL,
matomoToken: process.env.MATOMO_TOKEN
}));
}
app.use(ua.middleware("UA-115902726-4", { cookieName: '_ga' }));
app.use(function (req, res, next) {
if (!req.headers['x-forwarded-for']) {
req.headers['x-forwarded-for'] = '0.0.0.0';
}
req.visitor.pageview({
dp: req.originalUrl,
dr: req.get('Referer'),
ua: req.headers['user-agent'],
uip: req.connection.remoteAddress
|| req.socket.remoteAddress
|| req.connection.remoteAddress
|| req.headers['x-forwarded-for'].split(',').pop()
}).send()
next();
});
app.use(favicon(path.join(__dirname, 'public/gi/img/favicon.ico'), { maxAge: oneDay }));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.use(methodOverride());
// development only
if ('development' == app.get('env')) {
app.use(errorHandler());
}
// Route endpoints
routes(app);
// all environments
app.use(ua.middleware(process.env.GA_TRACKING_ID, {cookieName: '_ga'}));
app.use(express.static(path.join(__dirname, 'public'), { maxAge: oneDay }));
app.use(require('uglify-js-middleware')({ src: path.join(__dirname,'public') }));
app.use(require('less-middleware')(path.join(__dirname,'public'), [], [], [{compress: true}]));
// Start server
app.listen(app.get('port'), function() {
console.log("Express server listening on port " + app.get('port'));
});
async function start() {
const app = express();
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
app.use(cookieParser());
// Setup Google Analytics
app.use(universalAnalytics.middleware(process.env.GA_ID, {cookieName: '_ga'}));
app.use(bodyParser());
app.use(logReqPerf);
const server = http.Server(app);
const io = require('socket.io')(server);
app.set('socketio', io);
// Init Nuxt.js
const nuxt = new Nuxt(config)
const {host, port} = nuxt.options.server
// Build only in dev mode
if (config.dev) {
const builder = new Builder(nuxt)
await builder.build()
const ga = (req, res, next) => {
// We generate a session to be able to keep track of requests coming from the same visitor
const session = expressSession({
httpOnly: true,
secret: 'b4;jP(cUqPaf8TuG@U',
cookie: {
secure: process.env.NODE_ENV === 'production' || process.env.NODE_ENV === 'staging',
maxAge: 60*60*24*30*1000 // 1 month
}
});
const mw = ua.middleware(config.GoogleAnalytics.account, {cookieName: '_ga'});
session(req, res, () => {
mw(req, res, next);
req.ga = {
pageview: () => req.visitor.pageview(req.url).send(),
event: (EventCategory, EventName, EventLabel, EventValue) => req.visitor.event(EventCategory, EventName, EventLabel, EventValue, {p: req.url}).send()
}
});
};
visitor.item(449.99, 1, 'ID54321', 'T-Shirt', 'Blue', { ti: '123456' }).send();
visitor.pageview('/').send();
visitor.pageview('/').pageview('/contact').send();
visitor.pageview('/landing-page-1').event('Testing', 'Button color', 'Blue').send();
visitor.pageview('/landing-page-1').send();
visitor.event('Testing', 'Button color', 'Blue', 42, { p: '/landing-page-1' }).send();
visitor
.event({ ec: 'Mail Server', ea: 'New Team Member Notification sent' })
.event({ ea: 'Invitation sent' })
.send();
visitor.set('uid', '123456789');
app.use(ua.middleware('UA-XXXX-Y', { cookieName: '_ga' }));
ua.createFromSession({ cid: 'some-string' });
ua('UA-XXXX-XX').debug();
ua('UA-XXXX-XX', {
requestOptions: {
proxy: '…',
},
});