Skip to content

Commit dd09d4c

Browse files
committed
Add the option to ignore empty account data
1 parent 6a5b131 commit dd09d4c

File tree

7 files changed

+309
-41
lines changed

7 files changed

+309
-41
lines changed

build/ga-api-utils.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/account-summaries/account-summaries.js

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@
3838
* management API's accountSummaries#list method.
3939
* @returns {AccountSummaries}
4040
*/
41-
function AccountSummaries(accounts) {
41+
function AccountSummaries(accounts, opts) {
42+
opts = opts || {};
4243

4344
this.accounts_ = accounts;
4445
this.webProperties_ = [];
@@ -49,6 +50,11 @@ function AccountSummaries(accounts) {
4950
this.profilesById_ = this.viewsById_ = {};
5051

5152
for (var i = 0, account; account = this.accounts_[i]; i++) {
53+
if (opts.ignoreEmpty && !accountHasAtLeastOneView(account)) {
54+
this.accounts_.splice(i, 1);
55+
i--;
56+
continue;
57+
}
5258

5359
this.accountsById_[account.id] = {
5460
self: account
@@ -60,6 +66,11 @@ function AccountSummaries(accounts) {
6066
alias(account, 'webProperties', 'properties');
6167

6268
for (var j = 0, webProperty; webProperty = account.webProperties[j]; j++) {
69+
if (opts.ignoreEmpty && !propertyHasAtLeastOneView(webProperty)) {
70+
account.webProperties.splice(j, 1);
71+
j--;
72+
continue;
73+
}
6374

6475
this.webProperties_.push(webProperty);
6576
this.webPropertiesById_[webProperty.id] = {
@@ -258,4 +269,30 @@ function alias(object, referenceProp, aliasName) {
258269
}
259270

260271

272+
/**
273+
* Accepts an account and returns true if it contains any properties with
274+
* views.
275+
* @param {Object} account An account summaries account object.
276+
* @return {boolean} true if the account contains at least one view.
277+
*/
278+
function accountHasAtLeastOneView(account) {
279+
if (account.webProperties && account.webProperties.length) {
280+
for (var i = 0, webProperty; webProperty = account.webProperties[i]; i++) {
281+
if (propertyHasAtLeastOneView(webProperty)) return true;
282+
}
283+
}
284+
return false;
285+
}
286+
287+
288+
/**
289+
* Accepts a property and returns true if it contains any views.
290+
* @param {Object} property An account summaries property object.
291+
* @return {boolean} true if the property contains at least one view.
292+
*/
293+
function propertyHasAtLeastOneView(property) {
294+
return !!(property.profiles && property.profiles.length);
295+
}
296+
297+
261298
module.exports = AccountSummaries;

lib/account-summaries/index.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,14 @@ function requestAccountSummaries() {
4545

4646
return new promise.constructor(function(resolve, reject) {
4747

48-
// Store the summaries array in the closure so multiple requests can
48+
// Store the accounts array in the closure so multiple requests can
4949
// concat to it.
50-
var summaries = [];
50+
var accounts = [];
5151

5252
promise.then(function fn(resp) {
5353
var result = resp.result;
5454
if (result.items) {
55-
summaries = summaries.concat(result.items);
55+
accounts = accounts.concat(result.items);
5656
}
5757

5858
if (result.startIndex + result.itemsPerPage <= result.totalResults) {
@@ -66,7 +66,7 @@ function requestAccountSummaries() {
6666
.then(fn);
6767
}
6868
else {
69-
resolve(new AccountSummaries(summaries));
69+
resolve(accounts);
7070
}
7171
})
7272
// Reject the promise if there are any uncaught errors;
@@ -88,12 +88,20 @@ module.exports = {
8888
* return it to avoid multiple requests. If the promise does not exist,
8989
* initiate the request and cache the promise.
9090
*
91-
* @param {boolean} noCache When true make a request no matter what.
91+
* @param {Object} opts A config object.
9292
* @return {goog.Promise} A promise fulfilled with an AccountSummaries
9393
* instance.
9494
*/
95-
get: function(noCache) {
96-
if (noCache) cache = null;
97-
return cache || (cache = requestAccountSummaries());
95+
get: function(opts) {
96+
if (opts && opts.noCache) {
97+
cache = null;
98+
delete opts.noCache;
99+
}
100+
if (!cache) {
101+
cache = requestAccountSummaries();
102+
}
103+
return cache.then(function(accounts) {
104+
return new AccountSummaries(accounts, opts);
105+
});
98106
}
99107
};
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
{
2+
"kind": "analytics#accountSummaries",
3+
"username": "[email protected]",
4+
"totalResults": 5,
5+
"startIndex": 1,
6+
"itemsPerPage": 1000,
7+
"items": [
8+
{
9+
"id": "1001",
10+
"kind": "analytics#accountSummary",
11+
"name": "Account A",
12+
"webProperties": [
13+
{
14+
"kind": "analytics#webPropertySummary",
15+
"id": "UA-1001-1",
16+
"name": "WebProperty A.A",
17+
"internalWebPropertyId": "3001",
18+
"level": "STANDARD",
19+
"websiteUrl": "http://example.com",
20+
"profiles": [
21+
{
22+
"kind": "analytics#profileSummary",
23+
"id": "2001",
24+
"name": "Profile A.A.A",
25+
"type": "WEB"
26+
},
27+
{
28+
"kind": "analytics#profileSummary",
29+
"id": "2002",
30+
"name": "Profile A.A.B",
31+
"type": "WEB"
32+
},
33+
{
34+
"kind": "analytics#profileSummary",
35+
"id": "2003",
36+
"name": "Profile A.A.C",
37+
"type": "WEB"
38+
}
39+
]
40+
},
41+
{
42+
"kind": "analytics#webPropertySummary",
43+
"id": "UA-1001-2",
44+
"name": "WebProperty A.B",
45+
"internalWebPropertyId": "3002",
46+
"level": "STANDARD",
47+
"websiteUrl": "http://example.com",
48+
"profiles": [
49+
{
50+
"kind": "analytics#profileSummary",
51+
"id": "2004",
52+
"name": "Profile A.B.A",
53+
"type": "WEB"
54+
},
55+
{
56+
"kind": "analytics#profileSummary",
57+
"id": "2005",
58+
"name": "Profile A.B.B",
59+
"type": "WEB"
60+
},
61+
{
62+
"kind": "analytics#profileSummary",
63+
"id": "2006",
64+
"name": "Profile A.B.C",
65+
"type": "WEB"
66+
}
67+
]
68+
},
69+
{
70+
"kind": "analytics#webPropertySummary",
71+
"id": "UA-1001-3",
72+
"name": "WebProperty A.C",
73+
"internalWebPropertyId": "3003",
74+
"level": "STANDARD",
75+
"websiteUrl": "http://example.com",
76+
"profiles": [
77+
{
78+
"kind": "analytics#profileSummary",
79+
"id": "2007",
80+
"name": "Profile A.C.A",
81+
"type": "WEB"
82+
},
83+
{
84+
"kind": "analytics#profileSummary",
85+
"id": "2008",
86+
"name": "Profile A.C.B",
87+
"type": "WEB"
88+
},
89+
{
90+
"kind": "analytics#profileSummary",
91+
"id": "2009",
92+
"name": "Profile A.C.C",
93+
"type": "WEB"
94+
}
95+
]
96+
}
97+
]
98+
},
99+
{
100+
"id": "1002",
101+
"kind": "analytics#accountSummary",
102+
"name": "Account B",
103+
"webProperties": [
104+
{
105+
"kind": "analytics#webPropertySummary",
106+
"id": "UA-1002-1",
107+
"name": "WebProperty B.A",
108+
"internalWebPropertyId": "3004",
109+
"level": "STANDARD",
110+
"websiteUrl": "http://example.com",
111+
"profiles": [
112+
{
113+
"kind": "analytics#profileSummary",
114+
"id": "2010",
115+
"name": "Profile B.A.A",
116+
"type": "WEB"
117+
}
118+
]
119+
}
120+
]
121+
},
122+
{
123+
"id": "1003",
124+
"kind": "analytics#accountSummary",
125+
"name": "Account C",
126+
"webProperties": [
127+
{
128+
"kind": "analytics#webPropertySummary",
129+
"id": "UA-1003-1",
130+
"name": "WebProperty C.A",
131+
"internalWebPropertyId": "3005",
132+
"level": "STANDARD",
133+
"websiteUrl": "http://example.com",
134+
"profiles": [
135+
{
136+
"kind": "analytics#profileSummary",
137+
"id": "2011",
138+
"name": "Profile C.A.A",
139+
"type": "WEB"
140+
}
141+
]
142+
}
143+
]
144+
},
145+
{
146+
"id": "1006",
147+
"kind": "analytics#accountSummary",
148+
"name": "Account F",
149+
"webProperties": [
150+
{
151+
"kind": "analytics#webPropertySummary",
152+
"id": "UA-1006-2",
153+
"name": "WebProperty E.B",
154+
"internalWebPropertyId": "3008",
155+
"level": "STANDARD",
156+
"websiteUrl": "http://example.com",
157+
"profiles": [
158+
{
159+
"kind": "analytics#profileSummary",
160+
"id": "2012",
161+
"name": "Profile E.B.A",
162+
"type": "WEB"
163+
}
164+
]
165+
}
166+
]
167+
}
168+
]
169+
}

test/_fixtures/account-summaries.json

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@
105105
"kind": "analytics#webPropertySummary",
106106
"id": "UA-1002-1",
107107
"name": "WebProperty B.A",
108-
"internalWebPropertyId": "3002",
108+
"internalWebPropertyId": "3004",
109109
"level": "STANDARD",
110110
"websiteUrl": "http://example.com",
111111
"profiles": [
@@ -128,7 +128,7 @@
128128
"kind": "analytics#webPropertySummary",
129129
"id": "UA-1003-1",
130130
"name": "WebProperty C.A",
131-
"internalWebPropertyId": "3003",
131+
"internalWebPropertyId": "3005",
132132
"level": "STANDARD",
133133
"websiteUrl": "http://example.com",
134134
"profiles": [
@@ -145,22 +145,53 @@
145145
{
146146
"id": "1004",
147147
"kind": "analytics#accountSummary",
148-
"name": "Account D (Property-less)"
148+
"name": "Account D"
149149
},
150150
{
151151
"id": "1005",
152152
"kind": "analytics#accountSummary",
153-
"name": "Account E (View-less)",
153+
"name": "Account E",
154154
"webProperties": [
155155
{
156156
"kind": "analytics#webPropertySummary",
157157
"id": "UA-1005-1",
158-
"name": "WebProperty E.A (View-less)",
159-
"internalWebPropertyId": "3005",
158+
"name": "WebProperty E.A",
159+
"internalWebPropertyId": "3006",
160160
"level": "STANDARD",
161161
"websiteUrl": "http://example.com"
162162
}
163163
]
164+
},
165+
{
166+
"id": "1006",
167+
"kind": "analytics#accountSummary",
168+
"name": "Account F",
169+
"webProperties": [
170+
{
171+
"kind": "analytics#webPropertySummary",
172+
"id": "UA-1006-1",
173+
"name": "WebProperty E.A",
174+
"internalWebPropertyId": "3007",
175+
"level": "STANDARD",
176+
"websiteUrl": "http://example.com"
177+
},
178+
{
179+
"kind": "analytics#webPropertySummary",
180+
"id": "UA-1006-2",
181+
"name": "WebProperty E.B",
182+
"internalWebPropertyId": "3008",
183+
"level": "STANDARD",
184+
"websiteUrl": "http://example.com",
185+
"profiles": [
186+
{
187+
"kind": "analytics#profileSummary",
188+
"id": "2012",
189+
"name": "Profile E.B.A",
190+
"type": "WEB"
191+
}
192+
]
193+
}
194+
]
164195
}
165196
]
166197
}

0 commit comments

Comments
 (0)