Skip to content

GA-Google-Analytics-Data-to-SalesForce is a project designed to seamlessly integrate Google Analytics data into Salesforce, enhancing business insights and optimizing marketing strategies. By leveraging the strengths of both platforms, this integration allows for the extraction of crucial data from Google Analytics using APIs or data connectors and

Notifications You must be signed in to change notification settings

Sorab0428/GA-Google-Analytics-Data-to-SalesForce

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 

Repository files navigation

GA-Google-Analytics-Data-to-SalesForce

Introduction

GA-Google-Analytics-Data-to-SalesForce is a project focused on integrating data from Google Analytics into Salesforce. This integration aims to harness the strengths of both platforms, enhancing business insights and optimizing marketing strategies. The process involves extracting pertinent data from Google Analytics using APIs or data connectors and seamlessly importing it into Salesforce. This unified dataset empowers users to generate custom reports, build insightful dashboards, and establish automated workflows within Salesforce, thereby maximizing operational efficiency and decision-making capabilities.

What is Google-Analytics(GA)

Google Analytics is a powerful web analytics service offered by Google that helps website owners and marketers track and analyze various aspects of website traffic and user behavior.

Key Features

  1. Traffic Analysis: Tracks the number of visitors, page views, and sessions on a website over time. It distinguishes between new and returning visitors.

  2. Audience Insights: Provides demographic information (age, gender, interests) and geographic data (location) about website visitors.

  3. Behavioral Analysis: Shows how users navigate through the site, which pages they visit, how long they stay on each page, and where they exit.

  4. Conversion Tracking: Allows setting up goals (e.g., purchases, sign-ups) and tracking their achievement.

  5. Acquisition Channels: Identifies traffic sources (search engines, social media, referrals) and evaluates marketing campaign effectiveness.

  6. Customization and Reporting: Enables creation of custom reports, dashboards, and segments for in-depth analysis.

How Google Analytics Works

Google Analytics works by placing a small tracking code on each page of a website. This code collects visitor data and sends it to Google's servers for processing. The insights derived from Google Analytics help businesses understand their audience better, improve user experience, and optimize marketing strategies.

For more detailed information and setup instructions, visit the Google Analytics website.

Before You Start

Make sure you have the following information.

a Google Analytics account.

Apply for a Google Analytics account

a service account.

Apply for a service account

How to obtain GA report two methods

1.Using Client Libraries

Create a service account from an existing project or quickly create a project in the Reference section, and ensure you have a service account and a JSON key.

Reference:Using Client Libraries doc

The following will be demonstrated using Node.js.

Set the environment variable to the service account's JSON key file.

// Service account credentials
process.env["GOOGLE_APPLICATION_CREDENTIALS"] =
  "your ServiceAccountCredentials.json";

Set the GA resource ID for querying data and load the required modules.

// Google Analytics 4 property ID
propertyId = "your propertyId";

// Imports the Google Analytics Data API client library.
const { BetaAnalyticsDataClient } = require("@google-analytics/data");
const analyticsDataClient = new BetaAnalyticsDataClient();

Set the query date range.

// The data import reports in Google Data API usually require 24 to 48 hours. Therefore, for accurate data, it is recommended to fetch data from two days ago.
const twoDaysAgo = new Date();
twoDaysAgo.setDate(twoDaysAgo.getDate() - 2);
const twoDaysAgoISO = twoDaysAgo.toISOString().split("T")[0];

Retrieve daily data.

Here's the code to retrieve daily active users and average page views per page

// Get daily data.
async function DailyData() {
  // AverageDailyTraffic
  const [responseAverageDailyTraffic] = await analyticsDataClient.runReport({
    property: `properties/${propertyId}`,
    dateRanges: [
      {
        startDate: twoDaysAgoISO,
        endDate: twoDaysAgoISO,
      },
    ],
    dimensions: [],
    metrics: [
      {
        name: "activeUsers",
      },
    ],
  });
  // AverageTimeOnPage
  const [responseAverageTimeOnPage] = await analyticsDataClient.runReport({
    property: `properties/${propertyId}`,
    dateRanges: [
      {
        startDate: twoDaysAgoISO,
        endDate: twoDaysAgoISO,
      },
    ],
    dimensions: [
      {
        name: "pageTitle",
      },
    ],
    metrics: [
      {
        name: "userEngagementDuration",
      },
      {
        name: "activeUsers",
      },
    ],
  });

  // AverageDailyTraffic
  let AverageDailyTraffic =
    responseAverageDailyTraffic.rows[0].metricValues[0].value;
  // data
  const dailyPageData = responseAverageTimeOnPage.rows;
  // send data to SF
  sendRequestToSF(AverageDailyTraffic, dailyPageData);
}

Send data to SF.

function sendRequestToSF(AverageDailyTraffic, dailyPageData) {
  const url = "your salasForce endpoint/services/apexrest/GA_Test_GetData";
  const data = {
    twoDaysAgo: twoDaysAgoISO,
    dailyTraffic: AverageDailyTraffic,
    pageData: dailyPageData,
  };
  fetch(url, {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
    },
    body: JSON.stringify(data),
  })
    .then((response) => {
      if (!response.ok) {
        throw new Error("Network response was not ok");
      }
      return response.json();
    })
    .then((data) => {
      console.log(data);
    })
    .catch((error) => {
      console.error("Fetch error:", error);
    });
}
// Execute
DailyData();

Execute this program using Node.js.

// Open CMD and execute.
node your project.js

2.using the REST API

Ensure that your project has OAuth 2.0 keys available for use.

Apply for or quickly set up a new OAuth 2.0 keys from an existing project or using the references below. Reference:using the command line doc

Install and initialize the Cloud SDK

Install the gcloud CLI

Open CMD with the OAuth JSON file obtained above.

// Authenticate
gcloud auth application-default login \
    --scopes=https://www.googleapis.com/auth/analytics.readonly \
    --client-id-file=[PATH/TO/credentials.json]

Modify the input format to the following:

gcloud auth application-default login --scopes=https://www.googleapis.com/auth/analytics.readonly --client-id-file=[PATH/TO/credentials.json]

Afterward, a prompt will appear to log in to your Google account, select the project owner, and agree to Google's terms. Upon successful completion, CMD will display a prompt providing us with a JSON key package.

get access_token

Then, use POSTman or another method to send a POST request to https://oauth2.googleapis.com/token to request an access_token.

*header Content-Type:application/x-www-form-urlencoded

*body

"client_id":your client_id,
"client_secret":your client_secret,
"refresh_token":your refresh_token,
"grant_type":refresh_token

*The response will like that

{
"access_token": "your access_token",
"expires_in": 3599,
"scope": "https://www.googleapis.com/auth/analytics.readonly",
"token_type": "Bearer"
}

get GA4 data

Subsequently, you can retrieve data by sending a POST request to https://analyticsdata.googleapis.com/v1beta/properties/{resourceID}:runReport.

*header

Content-Type:application/json
Authorization:"Bearer " + "your access_token"

*body

{
    "dateRanges": [
    {
        "startDate": "2024-06-02",
        "endDate": "2024-06-02"
    }
    ],
    "dimensions": [

    ],
    "metrics": [

    {
        "name": "activeUsers"
    }
    ]
}

Written using apex class

structure

public class refreshData {
    public String access_token;
    public String expires_in;
    public String scope;
    public String grant_type;
}
public class Data {
    public List<Rows> rows;
}
public class Rows {
    public List<DimensionValue> dimensionValues;
    public List<MetricValue> metricValues;
}
public class DimensionValue {
    public String value;
    public String oneValue;
}
public class MetricValue {
    public String value;
    public String oneValue;
}

Refresh the refreshToken to obtain an access token.

String clientId = 'your clientId';
String clientSecret = 'your clientSecret';
String refreshToken = 'your refreshToken';
String grantType = 'refresh_token';
// Setting up Http
Http refreshTokenhttp = new Http();
HttpRequest refreshTokenRequest = new HttpRequest();
refreshTokenRequest.setEndpoint('https://oauth2.googleapis.com/token');
refreshTokenRequest.setMethod('POST');
// Header
refreshTokenRequest.setHeader('Content-Type', 'application/x-www-form-urlencoded');
// Body
String requestBody = 'client_id=' + EncodingUtil.urlEncode(clientId, 'UTF-8')
    + '&client_secret=' + EncodingUtil.urlEncode(clientSecret, 'UTF-8')
    + '&refresh_token=' + EncodingUtil.urlEncode(refreshToken, 'UTF-8')
    + '&grant_type=' + EncodingUtil.urlEncode(grantType, 'UTF-8');
refreshTokenRequest.setBody(requestBody);
// Sending request
HttpResponse refreshTokenResponse = refreshTokenhttp.send(refreshTokenRequest);
System.debug(refreshTokenResponse.getStatusCode());
System.debug(refreshTokenResponse.getBody());

Retrieve GA reports using the access token.

Http accessTokenHttp1 = new Http();
HttpRequest accessTokenRequest1 = new HttpRequest();
accessTokenRequest1.setEndpoint('https://analyticsdata.googleapis.com/v1beta/properties/270166080:runReport');
accessTokenRequest1.setMethod('POST');
accessTokenRequest1.setHeader('Content-Type', 'application/json');
accessTokenRequest1.setHeader('Authorization', 'Bearer ' + refreshTokenData.access_token);

// Create a dateRanges list and add a date range
List<Map<String, String>> dateRanges = new List<Map<String, String>>();
Map<String, String> dateRange = new Map<String, String>();
dateRange.put('startDate', twoDaysAgoISO);
dateRange.put('endDate', twoDaysAgoISO);
dateRanges.add(dateRange);

// Create a dimensions list and add a dimension (this is an empty array for fetching overall active users)
List<Map<String, String>> dimensions = new List<Map<String, String>>();

// Create a metrics list and add a metric
List<Map<String, String>> metricsses = new List<Map<String, String>>();
Map<String, String> metrics = new Map<String, String>();
metrics.put('name', 'activeUsers');
metricsses.add(metrics);

// Create a JSON object
Map<String, Object> jsonBody = new Map<String, Object>();
jsonBody.put('dateRanges', dateRanges);
jsonBody.put('dimensions', dimensions);
jsonBody.put('metrics', metricsses);

// Convert to JSON string
String jsonString1 = JSON.serialize(jsonBody);
accessTokenRequest1.setBody(jsonString1);
HttpResponse accessTokenresponse1 = accessTokenHttp1.send(accessTokenRequest1);

Create records according to the custom object format.

Data data1 = (Data) JSON.deserialize(accessTokenresponse1.getBody(), Data.class);
Data data2 = (Data) JSON.deserialize(accessTokenresponse2.getBody(), Data.class);

for (Rows RowItem : data2.Rows) {
    GA__c newItem = new GA__c();
    newItem.name = twoDaysAgoISO;
    newItem.DailyTraffic__c = Integer.valueOf(data1.rows[0].metricValues[0].value);
    newItem.page__c = RowItem.dimensionValues[0].value;
    newItem.AverageTimeOnPage__c = Integer.valueOf(RowItem.metricValues[0].value) / Integer.valueOf(RowItem.metricValues[1].value);
    newItem.homePageActiveUsers__c = Integer.valueOf(RowItem.metricValues[1].value);
    insert newItem;
}

Ending

Integrating Google Analytics data into Salesforce through GA-Google-Analytics-Data-to-SalesForce offers a powerful opportunity to enhance business insights and marketing strategies. By combining the analytical depth of Google Analytics with Salesforce's robust CRM capabilities, organizations can derive actionable intelligence and optimize their digital marketing efforts.

Whether you opt for using client libraries or REST APIs for data extraction, the outlined process ensures a seamless integration that empowers users to create custom reports, insightful dashboards, and automated workflows within Salesforce. This integration not only enhances operational efficiency but also enables data-driven decision-making across the organization.

Implementing GA-Google-Analytics-Data-to-SalesForce equips businesses with the tools to understand customer behavior more deeply, improve engagement strategies, and drive growth. Embrace this integration to unlock the full potential of your data assets and propel your business forward in the digital age.

About

GA-Google-Analytics-Data-to-SalesForce is a project designed to seamlessly integrate Google Analytics data into Salesforce, enhancing business insights and optimizing marketing strategies. By leveraging the strengths of both platforms, this integration allows for the extraction of crucial data from Google Analytics using APIs or data connectors and

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published