Using Google App Script to Copy Custom Dimensions

Subscribe to our monthly newsletter to get the latest updates in your inbox

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 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)
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)

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


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 , 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!