Using Google App Script to Copy Custom Dimensions

Copying a few dozen Custom Dimensions over to a new Google Analytics Property got you down? I know the feeling. If one of my clients acquires a company or buys a new site that needs its own GA property, the GA configuration and setup can be intense. I wish there were a way to apply custom dimensions to several properties at once, but there isn’t.

You have 200 Custom Dimension slots you can fill with GA 360—which is amazing—but if you have 157 dimensions already in place and need to manually replicate them, you will be clicking and typing in the dimension names and clicking some more for a very, very long time. (I’ve done it.)

The hero of this post? Google Apps Script. You can write a really simple script that uses the Google Analytics Management API to read all of the Custom Dimensions from a source property and copy them into a new property. Voila! Done!

Getting Started

Go to script.google.com/intro to start scripting. Google Apps Script will open a new project for you. Title it.

Writing Your Script

First we want to establish what our Source Property and Source Account are — this is where we are copying our dimensions from. You will find your Property ID in the Property settings in the Admin panel of Google Analytics.

function myFunction() {
  // Set values for GA Property and Account FROM which to copy custom dimensions
  var sourceProperty = 'UA-XXXXXXXXX-YY' 
  var sourceAccount = 'XXXXXXXXX' 

Next, we need to list the dimensions that exist in the source property. Refer to Google’s documentation here for additional context.

  // Set variable for dimensions from our source property
  var sourceDimensions = Analytics.Management.CustomDimensions.list(sourceAccount, sourceProperty)
  Logger.log(sourceDimensions) 

From the list, we want to extract the Source Dimensions:

  // Extract items list from sourceDimensions variable
  var items = sourceDimensions['items']

The next part of our code establishes the Destination Account and Destination Property to which we want to copy our Custom Dimensions.

  // Set values for GA Property and Account TO which to copy custom dimensions
    var destinationProperty = 'UA-XXXXXXXXX-ZZ' 
    var destinationAccount = sourceAccount // In this case, destination and source accounts are the same - update this value if your accounts are different

Then we are inserting all the the Custom Dimensions and their accompanying required fields into the Destination Property.

  // Loop through our custom dimensions in order to insert our list of custom dimension names, scopes, and active values into our new GA property
  for (var i = 0; i<items.length; i++){
    var resource = {'name':items[i]['name'],'scope':items[i]['scope'],'active':items[i]['active']}
    Analytics.Management.CustomDimensions.insert(resource, destinationAccount, destinationProperty)

  Logger.log(items[i])

The last piece of code ensures we don’t go over the API quota for the write requests limit of 1.5 queries per second per Account ID.

  // Pause for 1 second between adding each custom dimension, this ensures that the GA property has time to process the request fully before moving to the next custom dimension
  Utilities.sleep(1000) 
  }

 }

Running Your Script

I highly recommend that you create a test property to run this in before you use it with your actual destination property. Be sure to plug in your test property’s UA number as your Destination Property in your code.

When you’re ready to run it, click Run > My Function.

You’ll need to have edit access to the GA properties you are referencing in your script, and approve access to GA.

If you have never enabled Google Apps Script before, you will need to do that as well.

In the script editor, go to Resources > Advanced Google services…

A dialogue box will appear. Toggle the Google Analytics API “on” if it isn’t on already.

Before you click “OK” you will see a note that says, “These services must also be enabled in the Google API Console.”

If you click the link, it should bring you over to https://console.developers.google.com, where you can enable.

 

Google Apps Script

See Ellie’s Google App Script in full here.

Now you have the tools to get started writing your own Google Apps Script! I’d love to hear how you’re using the Google Analytics Management API to more efficiently set up and maintain your Accounts. Happy Scripting!

4 Comments

  1. Nice post 🙂
    Note that the API quota issue is not so much about requests/second but rather requests/day. You can hit the quota limit by using the script on 3 properties in the same day so you’ll have to ask for an API quota extension.

    Reply
  2. Thanks Ellie nice post! I have tried it on my web properties and noticed that if you try to copy a CD set to a web property already having some CDs, the insert will actually append to what is already in place which is not what I would like since the index is very important. So I adjusted the script to check if the index already exist, update else insert.

    See below:

    if (destinationItems[i] != undefined){
    Analytics.Management.CustomDimensions.update(resource, destinationAccount, destinationProperty, cdName)
    } else {
    Analytics.Management.CustomDimensions.insert(resource, destinationAccount, destinationProperty)
    }

    Reply
    1. Thank you for optimizing the code Jean! 🙂

      I am keen on using this line “Analytics.Management.CustomDimensions.update(resource, destinationAccount, destinationProperty, cdName)” in my code to overwrite the existing list of 20 CDs with a new list.

      Can you tell me, what value must be passed in to the last parameter i.e. “cdName”?

      Reply
  3. Thank you Ellie! Just what I was looking for 🙂 Solved many of my problems in one go!

    Just as “Analytics.Management.CustomDimensions.insert(resource, destinationAccount, destinationProperty)” is used to insert a Custom Dimension, is there any option to update the existing list of Custom Dimensions?

    In particular, I am looking for an option to overwrite the existing 20 Custom Dimensions with a different list.

    Any help would be greatly appreciated! 🙂

    Reply

Leave a Comment