CData Python Connector for Google Analytics

Build 20.0.7587

Querying Data

After connecting with the steps in Connecting, you can query data using a session object.

Model Data Using a Mapping Class

Declare a mapping class for the table you wish to model in the ORM. This is done using "sqlalchemy.ext.declarative.declarative_base". A known table in the data model is modeled either partially or completely as in the below example:
import sqlalchemy.ext.declarative.declarative_base
Base = declarative_base()
class Traffic(Base):
	__tablename__ = "Traffic"
	Id = Column(String, primary_key=True)
	Browser = Column(String)
	DeviceCategory = Column(String)

Querying Data Using the Query Method

If the mapping class has been prepared, it is used alongside a session object to query the data source. After binding the engine to the session, simply provide the mapping class to the session's query method, as below:

engine = create_engine("googleanalytics:///?InitiateOAuth=GETANDREFRESH;Profile=myProfile;")
factory = sessionmaker(bind=engine)
session = factory()
for instance in session.query(Traffic).filter_by(Transactions="0"):
	print("Id: ", instance.Id)
	print("Browser: ", instance.Browser)
	print("DeviceCategory: ", instance.DeviceCategory)
	print("---------")

Querying Data Using the Execute Method

As an alternative, the session object can also run the query with the execute() method alongside the appropriate Table object. Assuming you have an active session, the below should be just as viable:

Traffic_table = Traffic.metadata.tables["Traffic"]
for instance in session.execute(Traffic_table.select().where(Traffic_table.c.Transactions == "0")):
	print("Id: ", instance.Id)
	print("FullName: ", instance.Name)
	print("City: ", instance.BillingCity)
	print("---------")

Copyright (c) 2020 CData Software, Inc. - All rights reserved.
Build 20.0.7587