Inserting a new Property issues

51 views
Skip to first unread message

jordan giebas

unread,
Jun 22, 2015, 8:16:58 AM6/22/15
to google-analytic...@googlegroups.com
So i'm working on a website for my university, which is currently a web application for one class. As of right now, we have one section (being the summer), but come this fall, we will have multiple sections. I'm trying to make the application have as much interface as possible for the user so that everything they'll need to do will be local on the website. Right now, we have a google analytics account and one property (the one section), and I've used the embed api to create a little statistics page for that section; however, preparing for the upcoming semester, I need the user to just click a button and that will create a new property under the existing account which will correspond to a new section. I'm trying to use the Management API insert property to do so, but it doesn't seem to be working. I don't think it is an authorization problem because i'm doing it on the same page with which i did the statistics, and the embed api requires authorization as well. Please, any help would be greatly appreciated.

- Jordan


Matthew Cohoon

unread,
Jun 22, 2015, 12:54:25 PM6/22/15
to google-analytic...@googlegroups.com

Hello Jordan
Write operations in the Management API (e.g. create, update, delete, patch) for Web Property, View (Profile), and Goal resources is currently available as a developer preview in limited beta. If you're interested in using these features, request access to the beta.

Please see the properties developer guide and reference guide for details.

-Matt

On Mon, Jun 22, 2015 at 5:16 AM, jordan giebas <jordan....@gmail.com> wrote:
So i'm working on a website for my university, which is currently a web application for one class. As of right now, we have one section (being the summer), but come this fall, we will have multiple sections. I'm trying to make the application have as much interface as possible for the user so that everything they'll need to do will be local on the website. Right now, we have a google analytics account and one property (the one section), and I've used the embed api to create a little statistics page for that section; however, preparing for the upcoming semester, I need the user to just click a button and that will create a new property under the existing account which will correspond to a new section. I'm trying to use the Management API insert property to do so, but it doesn't seem to be working. I don't think it is an authorization problem because i'm doing it on the same page with which i did the statistics, and the embed api requires authorization as well. Please, any help would be greatly appreciated.

- Jordan


--
You received this message because you are subscribed to the Google Groups "Google Analytics Management API" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-analytics-mana...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

jordan giebas

unread,
Jun 23, 2015, 1:09:50 PM6/23/15
to google-analytic...@googlegroups.com
Hi thanks for the response Matt. Actually, before I became a part of this group I requested access to limited beta from the same url. I received an email from Pete at google and it said

"Great news! We've received your request to join the Google Analytics Account Setup and Configuration APIs Beta and your project has been whitelisted for access. These APIs allow you to programmatically create and edit properties, profiles, and goals"

So I believe I do have access, I'm just not sure how to implement this now and to insert properties through the web management api. 

Thanks again,
Jordan Giebas
To unsubscribe from this group and stop receiving emails from it, send an email to google-analytics-management-api+unsubscribe@googlegroups.com.

Matthew Cohoon

unread,
Jun 23, 2015, 2:53:29 PM6/23/15
to google-analytic...@googlegroups.com
Hi Jordan,
Thats great that you've signed up for the beta, With that you should be able to implement feature you described -- Creating a new property with the Management API. To do that you will need to call the analytics.management.webproperties.insert endpoint.

function insertProperty(accountId, url, name) {
  var request = gapi.client.analytics.management.webproperties.insert(
    {
      'accountId': accountId,
      'resource': {
        'websiteUrl': url,
        'name': name
      }
    });
  request.execute(function (response) { // Handle the response. });
}

You will also need to change your authentication code to account for the new scope:
https://www.googleapis.com/auth/analytics.edit

gapi.analytics.auth.authorize({
  container: 'auth-button',
  clientid: CLIENT_ID,
  scopes: ["https://www.googleapis.com/auth/analytics.edit",
           "https://www.googleapis.com/auth/analytics.readonly"]
});

Hopefully this helps, but if you have an actual error message that might help narrow down where your problem rests.
Matt

To unsubscribe from this group and stop receiving emails from it, send an email to google-analytics-mana...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Google Analytics Management API" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-analytics-mana...@googlegroups.com.

jordan giebas

unread,
Jun 26, 2015, 7:38:00 AM6/26/15
to google-analytic...@googlegroups.com
Matt,
(Sorry for the late response, took personal days off work). 

So i currently have the authorization for the embed api in another file that's path is specified into two script tags in the html header. The authorization part I just turned into it's own function "authorizeUser()" (js), and when the body of the page loads, I initiate the activate user and it works fine for my stats page. Now for this page, where I'd like to insert new web properties, I changed the scopes of the gapi.analytics.auth.authorize() like your post said to, and still put its path in between <script> tags, then when the body loads, i expected it to the authorize the user by the same mechanism. Furthermore, I created a button on this new page, where when you click it, it activates the function "insertProperty(accountID, url, name)". (For now, I hardcoded these parameters into the function just to see if it would work, but later i would expect them to fill this in previously and then it will automatically be passed to the function as variables stored). When the button was clicked though, I saw no change on my GA account for a new web property. Any suggestions? One thing I was not so sure about when coding this for the first time was the format the url should be in, because if I use http://example.com, the // in javascript is the signal to comment, and appropriately comments out the rest.... 

Thanks again for all your help,
Sorry for the late response.

-Jordan 

Matthew Cohoon

unread,
Jun 26, 2015, 12:47:22 PM6/26/15
to google-analytic...@googlegroups.com
Hi Jordan,
The url needs to be the full path I believe. and if you use single quotes in JS you should be able encapsulate the // in a string:

    var url = 'https://www.google.com';

For debugging I would encourage you to use the JavaScript console (in chrome: View->Developer->JavaScript Console) and put break points around the insert call. See if the API returns an error or if it returns the resource object.

Feel free to post the error message you get back, and that should make it easier to debug.
-Matt



Reply all
Reply to author
Forward
0 new messages