Last active
October 7, 2018 15:26
-
-
Save dustinrecko/1586e3383d8bf6ec5777748c9872d733 to your computer and use it in GitHub Desktop.
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
/** | |
* Analytics Segment Provision in Tag Manager | |
* @author: Dustin Recko | |
* | |
*/ | |
var TAG_MANAGER_CONFIG = { | |
accountId: '', | |
containerId: '', | |
workspaceId: '' | |
}; | |
var ANALYTICS_CONFIG = { | |
profileId: "", // Your Analytics view id, e.g. "ga:137778740" | |
clientId: "" // The custom dimension where you store the Client Id, e.g. "ga:dimension4" | |
}; | |
var FIREBASE_CONFIG = { | |
url: "", // Your Database URL | |
auth: "" // Your Database Secret | |
}; | |
function main() { | |
init(); | |
var db = FirebaseApp.getDatabaseByUrl(FIREBASE_CONFIG.url,FIREBASE_CONFIG.auth); | |
var i = new UserSegments(); | |
i.segments = i.uaGetSegments(); | |
i.tmPatchVariables(); | |
for(var y = 0, segments = Object.keys(i.segments), len = segments.length; y<len; y++) { | |
i.uaGetUsers(segments[y]); | |
} | |
db.setData("",{}); | |
db.setData("", i.users); | |
} | |
function UserSegments() { | |
this.parent = "accounts/"+TAG_MANAGER_CONFIG.accountId+"/containers/"+TAG_MANAGER_CONFIG.containerId+"/workspaces/"+TAG_MANAGER_CONFIG.workspaceId; | |
this.segments = []; | |
this.users = {}; | |
// Analytics Functions | |
this.uaGetSegments = function() { | |
var list = {}; | |
var segments = Analytics.Management.Segments.list(); | |
for (var i = 0, segment; segment = segments.items[i]; i++) { | |
if(segment.created) // = Custom Segment | |
list[segment.segmentId] = segment.name | |
} | |
return list; | |
} | |
this.uaGetUsers = function(segmentId) { | |
var startDate = new Date().yyyymmdd(-90,'-'); | |
var endDate = new Date().yyyymmdd(0,'-'); | |
var tableId = ANALYTICS_CONFIG.profileId; | |
var metric = 'ga:sessions'; | |
var options = { | |
'dimensions': ANALYTICS_CONFIG.clientId, | |
'segment': segmentId, | |
'max-results': 10000 | |
}; | |
var report = Analytics.Data.Ga.get(tableId, startDate, endDate, metric, options); | |
if (report.rows) { | |
for(var i = 0, len = report.rows.length; i<len; i++) { | |
var clientId = report.rows[i][0].replace(/\./g,'_'); | |
if(this.users[clientId]) { | |
this.users[clientId].push(segmentId); | |
} else { | |
this.users[clientId] = [segmentId]; | |
} | |
} | |
} | |
} | |
// Tag Manager Functions | |
this.tmGetVariables = function() { | |
var arr = TagManager.Accounts.Containers.Workspaces.Variables.list(this.parent).variable; | |
var obj = {}; | |
for(var i=0, len = arr.length;i<arr.length;i++) { | |
obj[arr[i].name] = arr[i].variableId; | |
} | |
return obj; | |
} | |
this.tmPatchVariables = function() { | |
var existingVariables = this.tmGetVariables(); | |
for (var segmentId in this.segments) { | |
var value = this.segments[segmentId].replace(/^.*\sV([0-9]+)$/g,'$1'); | |
value = (value.match(/^[0-9]+$/g)) ? parseInt(value) : 0; | |
var name = "User Segment - "+this.segments[segmentId].replace(/[^0-9a-z\s]/gi,'').replace(/\sV[0-9]+$/g,'') | |
var variableResource = { | |
"name": name, | |
"type": "jsm", | |
"parameter": [{ | |
"type": "template", | |
"key": "javascript", | |
"value": "function() {\n if(window.gaIds === undefined) return 'n|0'; return (window.gaIds.indexOf(\""+segmentId+"\") > -1 ? 'y' : 'n')+'|"+value+"';\n}" | |
}] | |
}; | |
if(Object.keys(existingVariables).indexOf(name) > -1) { | |
TagManager.Accounts.Containers.Workspaces.Variables.update(variableResource, this.parent+'/variables/'+existingVariables[name]); | |
} else | |
TagManager.Accounts.Containers.Workspaces.Variables.create(variableResource, this.parent); | |
} | |
} | |
} | |
function init() { | |
Date.prototype.yyyymmdd = function(days,separator) { | |
separator = separator || ''; | |
if(days) { | |
this.setDate(this.getDate() + days); | |
} | |
return Utilities.formatDate(this, Session.getScriptTimeZone(), ['yyyy','MM','dd'].join(separator)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment