![]() |
VOOZH | about |
The CData Cmdlets for YouTube Analytics are standard PowerShell cmdlets that make it easy to accomplish data cleansing, normalization, backup, and other integration tasks by enabling real-time and bidirectional access to YouTube Analytics.
The Cmdlets are not only a PowerShell interface to YouTube Analytics, but also an SQL interface; this tutorial shows how to use both to create, retrieve, update, and delete YouTube Analytics data. We also show examples of the ADO.NET equivalent, which is possible with the CData ADO.NET Provider for YouTube Analytics. To access YouTube Analytics data from other .NET applications, like LINQPad, use the CData ADO.NET Provider for YouTube Analytics.
Once you have acquired the necessary connection properties, accessing YouTube Analytics data in PowerShell can be enabled in three steps.
YouTube Analytics uses the OAuth authentication standard. You can use the embedded CData OAuth credentials or you can register an application with Google to obtain your own.
In addition to the OAuth values, to access YouTube Analytics data set ChannelId to the Id of a YouTube channel. You can obtain the channel Id in the advanced account settings for your channel. If not specified, the channel of the currently authenticated user will be used.
If you want to generate content owner reports, specify the ContentOwnerId property. This is the Id of the copyright holder for content in YouTube's rights management system. The content owner is the person or organization that claims videos and sets their monetization policy.
Install the module:
Install-Module YouTubeAnalyticsCmdlets
Connect:
$youtubeanalytics = Connect-YouTubeAnalytics -ContentOwnerId "$ContentOwnerId" -ChannelId "$ChannelId" -InitiateOAuth "$InitiateOAuth"
Search for and retrieve data:
$mine = "True" $groups = Select-YouTubeAnalytics -Connection $youtubeanalytics -Table "Groups" -Where "Mine = `'$Mine`'" $groups
You can also use the Invoke-YouTubeAnalytics cmdlet to execute SQL commands:
$groups = Invoke-YouTubeAnalytics -Connection $youtubeanalytics -Query 'SELECT * FROM Groups WHERE Mine = @Mine' -Params @{'@Mine'='True'}
Load the provider's assembly:
[Reflection.Assembly]::LoadFile("C:\Program Files\CData\CData ADO.NET Provider for YouTube Analytics\lib\System.Data.CData.YouTubeAnalytics.dll")
Connect to YouTube Analytics:
$conn= New-Object System.Data.CData.YouTubeAnalytics.YouTubeAnalyticsConnection("ContentOwnerId=MyContentOwnerId;ChannelId=MyChannelId;InitiateOAuth=GETANDREFRESH;")
$conn.Open()
Instantiate the YouTubeAnalyticsDataAdapter, execute an SQL query, and output the results:
$sql="SELECT Snippet_Title, ContentDetails_ItemCount from Groups"
$da= New-Object System.Data.CData.YouTubeAnalytics.YouTubeAnalyticsDataAdapter($sql, $conn)
$dt= New-Object System.Data.DataTable
$da.Fill($dt)
$dt.Rows | foreach {
Write-Host $_.snippet_title $_.contentdetails_itemcount
}
Update-YouTubeAnalytics -Connection $YouTubeAnalytics -Columns @('Snippet_Title','ContentDetails_ItemCount') -Values @('MySnippet_Title', 'MyContentDetails_ItemCount') -Table Groups -Id "MyId"
$cmd = New-Object System.Data.CData.YouTubeAnalytics.YouTubeAnalyticsCommand("UPDATE Groups SET Mine='True' WHERE Id = @myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.YouTubeAnalytics.YouTubeAnalyticsParameter("@myId","10456255-0015501366")))
$cmd.ExecuteNonQuery()
Add-YouTubeAnalytics -Connection $YouTubeAnalytics -Table Groups -Columns @("Snippet_Title", "ContentDetails_ItemCount") -Values @("MySnippet_Title", "MyContentDetails_ItemCount")
$cmd = New-Object System.Data.CData.YouTubeAnalytics.YouTubeAnalyticsCommand("INSERT INTO Groups (Mine) VALUES (@myMine)", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.YouTubeAnalytics.YouTubeAnalyticsParameter("@myMine","True")))
$cmd.ExecuteNonQuery()
Remove-YouTubeAnalytics -Connection $YouTubeAnalytics -Table "Groups" -Id "MyId"
$cmd = New-Object System.Data.CData.YouTubeAnalytics.YouTubeAnalyticsCommand("DELETE FROM Groups WHERE Id=@myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.YouTubeAnalytics.YouTubeAnalyticsParameter("@myId","001d000000YBRseAAH")))
$cmd.ExecuteNonQuery()
CodeProject
Download a free trial of the YouTube Analytics Data Provider to get started:
Download NowLearn more:
👁 YouTube Analytics IconEasy-to-use YouTube Analytics client enables .NET-based applications to easily consume YouTube Analytics Traffic, Sources, Demographics, Subscribers, etc.