我的应用程序中有一个 Collection View 。我想为每个 Collection View 单元格放置谷歌分析事件。这意味着,例如,当用户点击单元格时,应该触发 google analycis 事件。我已经为屏幕实现了谷歌分析(对于主屏幕)。但我没有附加图像的权限。
这是我的代码。
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CategoryCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionreuseIdentifier" forIndexPath:indexPath];
[cell addSubview:cell.categorylabel];
// [cell addSubview:cell.imageWebView];
Attachments *attch = [attachments objectAtIndex:indexPath.row];
cell.categorylabel.text =[NSString stringWithFormat:@" %@ ", attch.attachmenttitle];
[cell.categorylabel sizeToFit];
// [cell.imageWebView loadRequest:attch.request];
NSString *html = [NSString stringWithFormat:@"<html><head><style>body{padding:0; margin:0;}</style></head><body><img src='%@' width='800' height='800'></body></html>",attch.imageurl];
[cell.imageWebView loadHTMLString:html baseURL:nil];
self.tracker = [[GAI sharedInstance] defaultTracker];
[self.tracker send:[[GAIDictionaryBuilder createEventWithCategory:@"categories" action:attch.attachmenttitle label:attch.attachmenttitle value:[NSNumber numberWithInt:100]] build]];
return cell;
}
最佳答案
将 analytics
操作调用到 didSelectItemAtIndexPath
从此处的 cellForItemAtIndexPath
中删除以下行
self.tracker = [[GAI sharedInstance] defaultTracker];
[self.tracker send:[[GAIDictionaryBuilder createEventWithCategory:@"categories" action:attch.attachmenttitle label:attch.attachmenttitle value:[NSNumber numberWithInt:100]] build]];
将行添加到 didSelectItemAtIndexPath
- (void)tableView:(UICollectionView *)tableView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
NSArray*cells = [mainTable visibleCells];
UITableViewCell *currentcell = [mainTable cellForRowAtIndexPath:indexPath];
for (UITableViewCell*cell in cells)
{
if ([cell isEqual:currentcell] == NO)
{
}
else
{
self.tracker = [[GAI sharedInstance] defaultTracker];
[self.tracker send:[[GAIDictionaryBuilder createEventWithCategory:@"categories" action:attch.attachmenttitle label:attch.attachmenttitle value:[NSNumber numberWithInt:100]] build]];
}
}
}
关于ios - ios 中 uicollectionview 单元格的谷歌分析事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31423678/