Skip to content

Instantly share code, notes, and snippets.

@naranjja
Last active March 10, 2022 00:16
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save naranjja/eadf15573f31a9f1d6d4321eb0c0543f to your computer and use it in GitHub Desktop.
Save naranjja/eadf15573f31a9f1d6d4321eb0c0543f to your computer and use it in GitHub Desktop.
Google Analytics tracking using Dialogflow API v2 and some REST API
const ua = require("universal-analytics");
const request = require("request-promise");
const { https } = require("firebase-functions");
function getUserID (obj) {
if (!obj.source) {
return "dialogflow";
} else {
switch (obj.source) {
case "twilio":
return obj.payload.data.From;
case "facebook":
return obj.payload.data.sender.id;
default:
return "unknown";
}
}
}
function trackEvent (uid, category, action, label = null) { // label is optional
const visitor = ua("UA-XXXXXXXX-X", uid, { strictCidFormat: false }); // format is not strict
visitor.event(category, action, label, function (err) {
if (err) console.error(err);
})
}
function makeResponse(msg) {
return JSON.stringify({
"fulfillmentText": msg
});
}
function sampleAction(sampleInput, userID, res) {
request({
method: "GET",
uri: `http://sample.api/sampleEndpoint?sampleInput=${sampleInput}`,
json: true
})
.then((json) => {
if (json.result) {
trackEvent(userID, "actionSuccess", "sampleAction", `Found.`);
res.send(makeResponse("I found it!"));
} else {
trackEvent(userID, "actionFail", "sampleAction", "Not found.");
res.send(makeResponse("I couldn't find it!"));
}
})
.catch((err) => {
console.error(err);
trackEvent(userID, "actionFail", "sampleAction", "API error.");
res.send(makeResponse(`I had an issue with the API!`));
})
}
// 'dialogflowFirebaseFulfillment' is the name of the function that is exported
// cloud functions configuration executes this function and keeps it running as a webhook
exports.dialogflowFirebaseFulfillment = https.onRequest((req, res) => {
const { action, parameters } = req.body.queryResult;
const userID = getUserID(req.body.originalDetectIntentRequest);
switch (action) {
case "sampleAction":
trackEvent(userID, "actionRequest", "sampleAction");
sampleAction(parameters.sampleInput, userID, res); // 'res' (response) object is passed for executing .send()
break;
default:
res.send(makeResponse([])); // empty array simply tells console to reply instead
}
})
{
"name": "dialogflowFirebaseFulfillment",
"description": "This is the default fulfillment for a Dialogflow agents using Cloud Functions for Firebase",
"version": "0.0.1",
"private": true,
"license": "Apache Version 2.0",
"author": "Google Inc.",
"engines": {
"node": "8"
},
"scripts": {
"start": "firebase serve --only functions:dialogflowFirebaseFulfillment",
"deploy": "firebase deploy --only functions:dialogflowFirebaseFulfillment"
},
"dependencies": {
"actions-on-google": "^2.2.0",
"firebase-admin": "^5.13.1",
"firebase-functions": "^2.0.2",
"request": "^2.88.0",
"request-promise": "^4.2.2",
"universal-analytics": "^0.4.20"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment