Skip to content

Instantly share code, notes, and snippets.

View digitalbase's full-sized avatar

Gijs Nelissen digitalbase

View GitHub Profile
{
"anonymousId": "a5b62615-cf5e-4e1c-bd01-a9852914baaa",
"context": {
"campaign": {
"medium": "ppc",
"name": "High Intent Keywords",
"source": "adwords",
"term": ""
},
"ip": "8.8.8.8",
const AWS = require('aws-sdk');
const { DocumentClient } = require('aws-sdk/clients/dynamodb');
module.exports = () => {
return new DocumentClient( {
// ensures empty values (userId = null) are converted
// more @ https://stackoverflow.com/questions/37479586/nodejs-with-dynamodb-throws-error-attributevalue-may-not-contain-an-empty-strin
convertEmptyValues: true
});
};
service: solving-marketing-attribution
custom:
# sma = Solving Marketing Attribution
tableIdentify: 'sma-identify-${self:provider.stage}'
tablePage: 'sma-event-page-${self:provider.stage}'
provider:
name: aws
runtime: nodejs10.x
const dynamoDBFactory = require('./src/dynamodb.factory');
const dynamoDb = dynamoDBFactory();
const { withStatusCode } = require('./src/utils/response.util');
const { parseWith } = require('./src/utils/request.util');
const parseJson = parseWith(JSON.parse);
const ok = withStatusCode(200);
const problem = withStatusCode(400);
service: segment-attribution
plugins:
- serverless-offline
custom:
tableIdentify: 'segment-event-identify-${self:provider.stage}'
tablePage: 'segment-event-page-${self:provider.stage}'
tableAttribution: 'segment-event-attribution-${self:provider.stage}'
tableUserMapping: 'segment-event-user-map-${self:provider.stage}'
const { unmarshallNewImageEvent } = require('./src/utils/dynamo_stream.util');
const dynamoDBFactory = require('./src/dynamodb.factory');
const { UserToAnonymousModel } = require('./src/models/UserToAnonymous');
const dynamoDb = dynamoDBFactory();
const model = new UserToAnonymousModel(dynamoDb);
module.exports.handler = async (event) => {
const eventData = event.Records[0];
const eventAddedUnmarshalled = unmarshallNewImageEvent(eventData);
//src/models/UserToAnonymous.js
const USER_MAP_TABLE = process.env.USER_MAP_TABLE;
class UserToAnonymousModel {
get _baseParams() {
return {
TableName: USER_MAP_TABLE
};
}
// /src/utils/dynamo_stream.util.js
const AWS = require('aws-sdk');
const unmarshallNewImageEvent = (eventData) => {
if (!eventData.dynamodb) {
throw new Error('Not a dynamo event');
}
// if you want the raw event we need to unmarshall it
// more @ https://stackoverflow.com/questions/44535445/unmarshall-dynamodb-json
// /src/utils/dynamo_stream.util.js
const AWS = require('aws-sdk');
const unmarshallNewImageEvent = (eventData) => {
if (!eventData.dynamodb) {
throw new Error('Not a dynamo event');
}
// if you want the raw event we need to unmarshall it
// more @ https://stackoverflow.com/questions/44535445/unmarshall-dynamodb-json
// src/utils/referrer_detection.util.js
const inbound = require('prezly-inbound');
module.exports = async(href, referrer) => {
console.log('going extraction', href, referrer);
return new Promise((resolve, reject) => {
inbound.referrer.parse(href, referrer, (error, data) => {
if (error) {