Skip to content

Instantly share code, notes, and snippets.

View chipoglesby's full-sized avatar
🏠
Working from home

Chip Oglesby chipoglesby

🏠
Working from home
View GitHub Profile
@chipoglesby
chipoglesby / mccspend.js
Last active August 29, 2015 14:22
An Adwords MCC script that pulls accounts from a spreadsheet and then writes the account spend for the month on another sheet.
var dateRange = "THIS_MONTH";
var SPREADSHEET_URL = "url";
var accounts = [];
var values = [];
function main() {
var ss = SpreadsheetApp.openByUrl(SPREADSHEET_URL);
var clear = ss.getSheetByName('Adwords').getRange("A2:C").clear();
var sheet = ss.getSheetByName('Accounts');
var name = sheet.getName();
@chipoglesby
chipoglesby / scrolltracking.js
Last active December 30, 2018 07:43
Tracking Scroll Depth
<script>
/*!
* @preserve
* jquery.scrolldepth.js | v0.4.1
* Copyright (c) 2014 Rob Flaherty (@robflaherty)
* Licensed under the MIT and GPL licenses.
*/
;(function ( $, window, document, undefined ) {
"use strict";
@chipoglesby
chipoglesby / facebookads.gs
Created June 23, 2015 03:39
Pull Facebook Advertising Metrics into Google Sheets
var SPREADSHEET_URL = "";
function facebookSpend() {
var ss = SpreadsheetApp.openByUrl(SPREADSHEET_URL);
var sheet = ss.getSheetByName('facebook');
var values = [];
Utilities.sleep(1000);
var fullUrl = 'https://graph.facebook.com/v2.3/act_xxx/stats?access_token=xxx';
var fetchRequest = UrlFetchApp.fetch(fullUrl);
var results = JSON.parse(fetchRequest.getContentText());
Logger.log(results.spent/100);
@chipoglesby
chipoglesby / ipaddresses.js
Created July 20, 2015 15:54
Blocking internal traffic in Google Tag Manager
<script>
function ipAddresses() {
var newIP = '{{visitorIP}}';
var patt = new RegExp("^28.28.128.1[0-9]?$");
if(patt.test(newIP) === true) {return('internal');}
var patt = new RegExp("^65.65.65.12[0-9]?$");
if(patt.test(newIP) === true) {return('monitoring');}
return('external');
}
</script>
@chipoglesby
chipoglesby / getdistance.gs
Created August 4, 2015 19:15
Get Distance between two points in Google Sheets with the Maps API
function getDirection(city1, city2) {
var directions = Maps.newDirectionFinder()
.setOrigin(city1).setDestination(city2)
.setMode(Maps.DirectionFinder.Mode.DRIVING)
.getDirections();
var d = directions.routes[0].legs[0].distance.text;
return parseInt(d.split(" ")[0].replace(",", ""));;
@chipoglesby
chipoglesby / geocode_addresses.gs
Created August 4, 2015 19:16
Geocode postal addresses
function geocode_Addresses() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var locationInfo = sheet.getRange(1, 1, sheet.getLastRow() - 1, 1).getValues();
var geoResults, lat, lng;
for (var i = 0; i < locationInfo.length; i++) {
geoResults = Maps.newGeocoder().geocode(locationInfo[i]);
// Get the latitude and longitude
lat = geoResults.results[0].geometry.location.lat;
@chipoglesby
chipoglesby / googlemaps_sheets.gs
Created August 4, 2015 19:17
Get maps info in Google Sheets.
/* Get the Google Maps Link */
/* The longitude and latitude are passed as a comma separated string */
function getGoogleMapsLink(longLat) {
return "https://maps.google.com/maps?q="+longLat;
}
/* Get the Postal Address from geo location */
function getStreetAddress(longLat) {
var longLat = longLat.split(',');
var response = Maps.newGeocoder().reverseGeocode(longLat[0], longLat[1]);
@chipoglesby
chipoglesby / costdata.gs
Last active November 29, 2020 19:27
Cost Data Upload via Google Analytic's Management API with Google Sheets
function uploadData() {
var accountId = "xxxxxxxx";
var webPropertyId = "UA-xxxxxxxx-x";
var customDataSourceId = "xxxxxxxx";
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var maxRows = ss.getLastRow();
var maxColumns = ss.getLastColumn();
var data = [];
for (var i = 1; i <= maxRows;i++) {
data.push(ss.getRange([i], 1,1, maxColumns).getValues());
@chipoglesby
chipoglesby / analytics.gs
Created September 24, 2015 14:52
Querying Google Analytics with Adwords Scripts
function main() {
var profileId = 'xxxxxxx';
// Dates should be in yyyy-mm-dd format.
var startDate = 'yesterday';
var endDate = 'today';
var options = {
'dimensions': 'ga:source,ga:keyword',
'sort': '-ga:sessions,ga:source',
'filters': 'ga:medium==organic',
@chipoglesby
chipoglesby / custommenu.gs
Last active May 24, 2018 13:36
Custom File Menu for Google Apps Script
function onOpen() {
var array = [];
ss = SpreadsheetApp.getActiveSpreadsheet();
for (var n in ss.getSheets()) {
var sheets = ss.getSheets()[n];
array.push(sheets.getName());
}
var ui = SpreadsheetApp.getUi();
ui.createMenu('Trip Menu')
.addItem('Trips', 'menuItem1')