Created
March 11, 2018 13:32
-
-
Save RitwikGA/260ff9a867529efb330db39dc0ebc9ab to your computer and use it in GitHub Desktop.
TypeForm Tracking in Google Analytics
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Typform Tracking in Google Analytics. (Free Account) | |
* @Ritwikga | |
* Digishuffle | |
*/ | |
//// Create Webhook For Every New Form////// | |
var formId = '' // Form ID is available in the URL (typeform.com/form/{formID}/create) | |
var formTag = '' // Enter name for the webhook | |
/// Webhook & API Info //// | |
var webhookURL = '' //Enter the published web app URL | |
var APIkey = '' //Personal Token from the account settings | |
///Google Anlaytics Info//////// | |
var propertyID = ''; //Google Analytics Property ID | |
var eventCategory = 'Form Submission'; | |
///////// Input End //////////////// | |
var mp_hit = { 'v':'1' , 'tid' : propertyID, 't': 'event','ni':'1', 'cid': Math.random().toString(16), 'ec':eventCategory } | |
var endpoint = "https://api.typeform.com/forms/"+formId+"/webhooks/"+formTag | |
function createWebhook() | |
{ | |
var payload = {url : webhookURL, enabled: true} | |
var response = UrlFetchApp.fetch(endpoint,{method:'put', | |
headers: {Authorization: 'bearer '+APIkey}, | |
payload:JSON.stringify(payload), | |
contentType:'application/json', | |
muteHttpExceptions:true}) | |
Logger.log(response) | |
} | |
function currentWebHook() | |
{ | |
var response = UrlFetchApp.fetch(endpoint,{method:'get',headers:{Authorization: 'bearer ' + APIkey},muteHttpExceptions:true }) | |
Logger.log(response) | |
} | |
function deleteWebHook() | |
{ | |
var response = UrlFetchApp.fetch(endpoint,{method:'delete',headers:{Authorization: 'bearer ' + APIkey},muteHttpExceptions:true }) | |
Logger.log(response) | |
} | |
function doPost(e) | |
{ | |
var parsedData = JSON.parse(e.postData.contents) | |
var formData = parsedData['form_response']['definition'] | |
var formTitle = formData['title'] + " ("+formData['id']+")" | |
mp_hit['ea'] = formTitle; | |
var url = 'https://www.google-analytics.com/collect' | |
var response = UrlFetchApp.fetch(url, {'method':'POST','payload':mp_hit}) | |
return HtmlService.createHtmlOutput("Success") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment