Skip to content

Instantly share code, notes, and snippets.

View benjimelito's full-sized avatar

Ben Melito (K-Ops CRM) benjimelito

View GitHub Profile
<script type="text/javascript">
var item = {
"$value": item.Price,
"ProductName": item.ProductName,
"ProductID": item.ProductID,
"SKU": item.SKU,
"Categories": item.Categories,
"ImageURL": item.ImageURL,
"URL": item.URL,
@benjimelito
benjimelito / sampleEvent.js
Created September 1, 2021 15:25
A sample "Send a Text" Klaviyo Event
var request = require("request")
var options = {
method: 'POST',
url: 'https://a.klaviyo.com/api/track',
headers: {
accept: 'text/html',
'content-type': 'application/x-www-form-urlencoded'
},
form: {
@benjimelito
benjimelito / rr_price_drop.py
Created June 2, 2021 03:43
Price Drop Lookup Script Example
#imports
from urllib.request import urlopen
import json
# variables
json_list = []
json_keys = {}
# 1. Open and serialize text file
text = open("data.txt", "r")
@benjimelito
benjimelito / metric_export.py
Created June 2, 2021 03:41
Klaviyo Metric Export/CSV Conversion
import requests
url = "https://a.klaviyo.com/api/v1/metric/METRIC_ID/timeline"
querystring = {"api_key":"PRIVATE_KEY","page":"0","count":"100"}
headers = {"Accept": "application/json"}
response = requests.request("GET", url, headers=headers, params=querystring)
data_frame = pandas.DataFrame(response.data)
data_frame.to_csv("~/CSV_LOCATION/CSV_NAME.csv")
@benjimelito
benjimelito / pushFromLocal.js
Last active April 29, 2023 23:06
Push events from local storage and send them to Klaviyo
if (Array.isArray(klaviyoCookie) && klaviyoCookie.length && localStorageAvailable()) {
klaviyoCookieData = JSON.parse(atob(klaviyoCookie[0].split("__kla_id=")[1]))
function klPushFromLocal(identifierKey, identifierValue) {
anonBackfillDebugLogger("| Pushing track and identify requests from local storage to Klaviyo...")
// run through all local identify & track requests and send to klaviyo
var klRequest = [];
var klResponse = "";
var localKlaviyo = JSON.parse(localStorage.getItem("localKlaviyo")) || [];
while (localKlaviyo.length > 0) {
if (!klaviyo.isIdentified()) {
// If the cookie doesn't contain an exchange ID, override the the push functionality for the klaviyo sdk
klaviyo.old_push = klaviyo.push
klaviyo.push = function (request) {
if (request[0] === "identify") {
// if someone attempts to identify the email address, unoverride push
if (Object.keys(request[1]).includes("$email")) {
klaviyo.push = klaviyo.old_push
klaviyo.push(["identify", { $email: request[1]["$email"] }])
// then push everything in localstorage as a raw request
@benjimelito
benjimelito / klLocalTrackIdentify.js
Last active April 29, 2023 22:59
Track and Identify calls using local storage
function klLocalIdentify(klPropertyPayload) {
// process and store a local identify request
var localKlaviyo = JSON.parse(localStorage.getItem("localKlaviyo")) || [];
var payload = {
token: PUBLIC_API_TOKEN,
properties: klPropertyPayload,
};
if (localKlaviyo.length == klQueuedRequestLimit) {
localKlaviyo.pop();
@benjimelito
benjimelito / klaviyoCookieTest.js
Last active May 19, 2021 17:21
Test to see if the user is identified by Klaviyo
var klaviyoCookie = document.cookie.split("; ").filter(function (c) {
return /__kla_id=/.test(c)
});
@benjimelito
benjimelito / pushEventsFromLocal.js
Last active April 29, 2023 22:39
A piece of a Javascript snippet pushing data to Klaviyo from local storage
if (!klaviyo.isIdentified()) {
// If the cookie doesn't contain an exchange ID, override the the push functionality for the klaviyo SDK
klaviyo.old_push = klaviyo.push
klaviyo.push = function (request) {
if (request[0] === "identify") {
// if someone attempts to identify the email address, unoverride push
if (Object.keys(request[1]).includes("$email")) {
klaviyo.push = klaviyo.old_push;
klaviyo.push(["identify", { $email: request[1]["$email"] }])
// then push everything in localstorage as a raw request
@benjimelito
benjimelito / checkForLocalStorage.js
Last active May 18, 2021 01:28
A function to test if local storage is available on a user's browser
function localStorageAvailable() {
var test = "test"
try {
localStorage.setItem(test, test)
localStorage.removeItem(test)
return true
} catch (e) {
return false
}
}