Skip to content

Instantly share code, notes, and snippets.

@jimcamut
jimcamut / lambdaEdgeJS.js
Last active November 2, 2021 22:22
A Lambda Edge handler for redirecting non-www to www for single-page JS apps
exports.handler = (event, context, callback) => {
let request = event.Records[0].cf.request;
const uri = request.uri;
const host = request.headers.host[0].value;
// Check whether the URI is missing a file name.
if (uri !== '/' && !uri.match(/\./gi) && !uri.endsWith('/')) {
request.uri += '/index.html';
}
(function initCookieLayer() {
// Set and get property id
// This is a unique id set on the property level
var propertyKey = 'mpp_uid';
var propertyId = initCookieByKey(propertyKey, true);
var propertySyncKey = 'mpp_uid_synced';
var propertySynced = getCookieByKey(propertySyncKey);
// right away update our data layer for GTM
updateDataLayer(propertyKey, propertyId)
(function initDataLayerWithUserId(key, newVal) {
/**
* This function is used to establish the GTM dataLayer with
* a predefined key/value pair. The end use case is to push
* an object like {userId: "abc123"} into the dataLayer. The
* extra logic to get here checks to see if there is already
* a cookie with the value saved.
*
* Parameters:
* key: <string> the key you want to set
@jimcamut
jimcamut / us-zips-matching-geo.json
Last active January 30, 2017 19:04 — forked from erichurst/US Zip Codes from 2013 Government Data
All US zip codes with their corresponding latitude and longitude coordinates.Comma delimited for your database goodness.Source: http://www.census.gov/geo/maps-data/data/gazetteer.html
This file has been truncated, but you can view the full file.
{
"00601": [18.180555, -66.749961],
"00602": [18.361945, -67.175597],
"00603": [18.455183, -67.119887],
"00606": [18.158345, -66.932911],
"00610": [18.295366, -67.125135],
"00612": [18.402253, -66.711397],
"00616": [18.420412, -66.671979],
"00617": [18.445147, -66.559696],
"00622": [17.991245, -67.153993],
var https = require('https');
exports.handler = function(event, context) {
var post_options = {
host: 'api.barkly.us',
path: '/parse/jobs/morning_job',
method: 'POST',
headers: {
'X-Parse-Application-Id': 'XXXXXXXXXXXXXXXXX',
@jimcamut
jimcamut / img-compress.js
Created June 11, 2016 15:10
Compress Images in Javascript - client side
function compressImg(source_img_obj, quality, maxWidth, output_format){
var mime_type = "image/jpeg";
if(typeof output_format !== "undefined" && output_format=="png"){
mime_type = "image/png";
}
maxWidth = maxWidth || 1000;
var natW = source_img_obj.naturalWidth;
var natH = source_img_obj.naturalHeight;
var ratio = natH / natW;