Last Updated on October 5, 2021 by Shivanandana Hegde
In this article, Firstly, I will explain how to copy custom dimensions in Google Analytics from one property to another property. Secondly, the best part is that, it can be used for multiple properties using a simple technique.
If you want to copy custom dimensions from one property to another, it can be done manually. However, for multiple properties, the manual process consumes a lot of time and effort. So, this method automates it and creates more efficiency.
Pre-Requisites for Copying Custom Dimensions in Google Analytics
- Google Universal Analytics Account:- You must have admin access for the Google Analytics account.
- Gmail Account:- Make sure that you have a Gmail ID through which you can access Google Analytics.
- Custom Dimensions:- At lease one of the property in your Google Analytics account should have the custom dimensions already created.
- Source Account ID and Property ID:- Account and Property ID refers to the IDs where the custom dimensions are already present.
- Destination Account ID and Property IDs:- This is nothing but the ID of the account and properties where you want to copy the dimensions.
Below are 4 simple steps to copy custom dimensions
Lets deep dive into how we can achieve this. Moreover, all you need is to follow few simple steps and we are good to go!
Step 1: Create a Project in Google Script. (It’s Free)
Visit https://www.google.com/script/start/ and click on “Start Scripting” as shown below.
Once you click on “Start Scripting” you must Sign In using the Gmail account.
Please note that the Google Analytics account should be linked with the Gmail ID.
Upon successful login , you will land on the Google Script Home Page. Click on “New Project”.
Step 2: Rename the project and add Google Analytics API as a Service.
Now click on Untitled Project and rename it as “Google Analytics Copy Dimensions”.
Once it is renamed, click on “+” icon beside the “Services” Menu as shown in above screenshot and select “Google Analytics API” and click “Add”.
We are now ready to add the actual logic/code to copy custom dimensions.
Step 3 to Copy Custom Dimensions: Add Google Script Code.
Add the below code into the code editor where you have created the project.
function copyDimensions() {
var gaSourceProperty = 'UA-XXXXXX-1'
var gaSourceAccount = 'Source account ID goes here'
var sourceDimensions = Analytics.Management.CustomDimensions.list(gaSourceAccount, gaSourceProperty)
var items = sourceDimensions['items']
var gaDestinationProperty = ['UA-XXXXXX-1','UA-XXXXXX-2','UA-XXXXXX-3','UA-XXXXXX-4']
var gaDestinationAccount = 'Destination Account ID goes here'
for(var j=0; j<gaDestinationProperty.length; j++){
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, gaDestinationAccount, gaDestinationProperty[j])
Utilities.sleep(1000)
}
}
}
Understanding of the Logic part
- “gaSourceProperty” variable is the Property ID of the Google Analytics account where you have already created the custom dimensions.
- “gaSourceAccount” variable is the Account ID of the Google Analytics account where you have already created custom dimensions.
- “gaDestinationProperty” variable consists of an array of properties where you wanna copy those custom dimensions.
- “gaDestinationAccount” variable holds the value of Account ID where yu want to copy the custom dimensions.
You can obtain these values from Google Analytics Admin Section. Please ensure that the Property and Account ID are correct. Else , you will end up getting the error while running the code.
- The variable “sourceDimensions” will hold the list of Dimensions from the source property.
- The variable “resource” holds name, scope and active status of the custom dimensions as shown below.
- The “insert” function inserts the custom dimensions into the required set of properties.
- Utilities.sleep(1000) ensures that there is a sufficient time to create dimensions before moving to the next property.
Step 4: Save and Run the Code.
Once you have modified the Account ID and Property ID details in the code, click on Save icon.
Now , Click on “Run”. If it asks for Permission, click on “Review Permission” and allow access.
As a result, you will see the Execution Log with a success message.
VOILA!!!! ? You have successfully copied the Custom Dimensions into required properties.
In conclusion:
- This code works only for Google Universal Analytics property. However, it won’t work on GA4 properties as of today.
- This code best suits when there are multiple properties under a single account.
If you want to learn how to install Analytics on your website, here is the link:- https://digilitica.com/solutions-and-services/web-analytics-solutions/install-analytics-across-website/
You can learn more about Google Analytics API using Google Script here:- https://developers.google.com/apps-script/advanced/analytics#reference